[使用纯Javascript或JQuery的Google OAuth 2访问令牌

问题描述 投票:0回答:1

我正在测试Google OAuth 2.0。我有我的客户ID,客户密码和验证码。现在,我想访问并刷新令牌。

但是,我可以从PHP做到这一点。但是使用Javascript时出现Invalid Grant错误。

    <!DOCTYPE html>
    <html>
    <head>
        <script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
    </head>

    <body>
        <a href='#' onClick='getAccessToken();'> Get your Access Token </a>
        <pre id="response"> </pre>

        <script>
            function getAccessToken() {

                $.ajax({
                    type: 'POST',
                    url: "https://accounts.google.com/o/oauth2/token",
                    contentType: 'application/x-www-form-urlencoded',
                    dataType: 'json',
                    data: {
                        client_id: 'MY CLIENT ID',
                        client_secret: 'MY SECRET',
                        code: 'MY AUTH CODE',
                        redirect_uri: 'http://localhost:8888/google-api.html',
                        grant_type: 'authorization_code'
                    },

                    success: function (data) {
                        $('#response').html(data);
                    },
                    error: function (e) {
                        $('#response').html(e.responseText);
                    }
                });

            }
        </script>

    </body>
    </html>

基本上,我正在尝试将下面的PHP Curl POST请求转换为Ajax

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,"https://accounts.google.com/o/oauth2/token");
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/x-www-form-urlencoded']);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
        'code'          => $code,
        'client_id'     => $client_id,
        'client_secret' => $client_secret,
        'redirect_uri'  => $redirect_uri,
        'grant_type'    => 'authorization_code',
    ]));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close ($ch);

注:堆栈溢出中类似类型的问题有很多观点,但仍未得到解答。天才的开发人员请提供您的意见。我也被困了三天。

javascript ajax google-api google-oauth2 google-api-client
1个回答
0
投票

感谢@Tankaike。在SO中,这个问题被问了3-4次,没有人认真回答。

初学者!!!需要注意的一点:Auth Code仅工作一次:)

        $.ajax({
            type: 'POST',
            url: "https://accounts.google.com/o/oauth2/token",
            contentType: 'application/x-www-form-urlencoded; charset=utf-8',
            crossDomain:true,
            cache : true, 
            dataType: 'json',
            data: {
                client_id: client_id,
                client_secret: client_secret,
                code: code,
                redirect_uri: redirect_uri,
                grant_type: grant_type,
            },

            success: function (data) {
                $('#response').html(JSON.stringify(data, null, " "));;
            },
            error: function (e) {
                $('#response').html(e.responseText);
            }
        });
© www.soinside.com 2019 - 2024. All rights reserved.