【问题标题】:Google OAuth: can't get refresh token with authorization codeGoogle OAuth:无法使用授权码获取刷新令牌
【发布时间】:2015-10-11 07:12:19
【问题描述】:

我正在使用带有 OAuth 2.0 的用于 Google Analytics 的 Google API 客户端

我阅读这篇文章是为了获取刷新令牌,但它没有出现:https://developers.google.com/identity/protocols/OAuth2WebServer#offline

我只得到这个:

{
 "access_token": "ya29.twHFi4LsiF-AITwzCpupMmie-fljyTIzD9lG8y_OYUdEGKSDL7vD8LPKIqdzRwvoQAWd",
 "token_type": "Bearer",
 "expires_in": 3599,
 "id_token": "very long string"
}

代码如下:

Javascript(获取授权码): 有效

gapi.analytics.ready(function() {

    gapi.analytics.auth.authorize({
        container: 'embed-api-auth-container',
        clientid: '257497260674-ji56vq885ohe9cdr1j6u0bcpr6hu7rde.apps.googleusercontent.com',
    });

    gapi.analytics.auth.on('success', function(response) {
        var code = response.code;
        $.ajax({
            url: "getTokensFromCode.php",
            method: "GET",
            data: {
                "code": code
            },
            success: function (tokens) {
                // I can't get refresh token here, only get "access_token" and "id_token"
                console.log(tokens);
            }
        });


    });

});

PHP(用授权码交换令牌): 这不起作用

// I get the authorization code in Javascript
$code = $_GET['code'];
$redirectURI = "postmessage";

$client = new Google_Client();
$client->setClientId($clientID);
$client->setClientSecret($clientSecret);
$client->setRedirectUri($redirectURI);
$client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
$client->authenticate($code);

$tokens = $client->getAccessToken();
echo $tokens;

我需要 Javascript 中的刷新令牌,这就是我在 Javascript 中获取授权代码并发出 Ajax 请求以获取刷新令牌的原因。

【问题讨论】:

    标签: google-api google-oauth google-analytics-api google-api-php-client google-api-client


    【解决方案1】:

    您只会在用户第一次授予您的应用访问权限时获得refresh_token。您需要将 refresh_token 存储在某个地方,以便以后可以使用它。下次用户登录您的应用时,您将不会获得新的刷新令牌。

    FWIW:在 Javascript 客户端中使用刷新令牌没有多大意义,因为 Javascript 客户端无法以安全(机密)的方式存储它。由于 Javascript 客户端存在于浏览器中并且用户存在于浏览器中,因此您只需将浏览器再次重定向到授权端点,您将获得一个新的访问令牌(这也是刷新令牌的目的)。 Google 用户的 SSO 会话将确保用户无需再次登录并且体验是无缝的。

    【讨论】:

    • 我需要授予一次访问权限,并且一直获取分析数据而无需再次登录(我需要向我网站的访问者显示数据)。这就是我想要refresh_token 的原因(access_token 仅持续 1 小时)。为了在第一次登录后获得refresh_token,我尝试了这个:$client->setAccessType('offline'); $client->setApprovalPrompt('force');
    • 我撤销了访问权限并再次登录,但它没有给我refresh_token。无论如何,我认为 JS-Ajax-PHP 的东西不起作用,我将尝试一个完整的 PHP 解决方案,如 Google 文档中所示。我尝试了这种方法,因为我想要 Javascript 中的refresh_token,但正如你所说,无论如何它都不安全。
    【解决方案2】:

    查看@curious_coder 的回复 Google API Refresh Token

    他解释了一种每次获取刷新令牌的方法 :) 救生员。这篇文章帮助我到达那里,所以我想我会与其他任何试图找到他们的刷新令牌的人分享这个。将此代码的最后两行添加到您的身份验证过程中

    $client = new Google_Client();
    $client->setAuthConfigFile(__DIR__ . '/client_secrets.json');
    $client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/dashboard/oauthcallbacks');
    $client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);    
    $client->setAccessType('offline'); //To fetch refresh token(Refresh token issued only first time)
    $client->setApprovalPrompt('force'); //Adding this will force for refresh token
    

    我还使用 var_dump($tokens) 来获取内容而不是 echo。不记得它是否对 PHP 有影响,但 $tokens 将是一个对象数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-24
      • 2012-10-14
      • 2018-06-23
      • 2016-10-13
      • 2020-07-26
      • 2015-11-26
      • 1970-01-01
      • 2017-09-23
      相关资源
      最近更新 更多