【问题标题】:How can I get an updated access token, using stored refresh token如何使用存储的刷新令牌获取更新的访问令牌
【发布时间】:2013-03-24 03:14:05
【问题描述】:

我正在构建一个应用程序,允许管理员验证对其分析帐户的访问权限以供离线使用,并将刷新令牌存储在数据库中。

现在当我尝试在前端使用 API 时,它返回以下错误:

"Access Token Expired. There wan a general error : The OAuth 2.0 access token has expired, and a refresh token is not available. Refresh tokens are not returned for responses that were auto-approved."

到目前为止,这是我产生此错误的代码:

require_once "lib/google/Google_Client.php";
require_once "lib/google/contrib/Google_AnalyticsService.php";

$_analytics = new analytics();
$_googleClient = new Google_Client();
$_googleClient->setClientId($_analytics->gaClientId);
$_googleClient->setClientSecret($_analytics->gaClientSecret);
$_googleClient->setRedirectUri($_analytics->gaRedirectUri);
$_googleClient->setScopes($_analytics->gaScope);
$_googleClient->setAccessType($_analytics->gaAccessType);

// Returns last access token from the database (this works)
$_tokenArray['access_token'] = $_analytics->dbAccessToken($_agencyId);
$_googleClient->setAccessToken(json_encode($_tokenArray));

if($_googleClient->isAccessTokenExpired()) {
    // Don't think this is required for Analytics API V3
    //$_googleClient->refreshToken($_analytics->dbRefreshToken($_agencyId));
    echo 'Access Token Expired'; // Debug
}

if (!$_googleClient->getAccessToken()) {
    echo '<h2>Error - Admin has not setup analytics correct yet</h2>';
}

我正在寻找一个运行 setRefreshToken 之类的函数 - 从数据库中输入值,管理员之前在线验证过它。

【问题讨论】:

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


    【解决方案1】:

    您可以尝试以下方法,您需要添加代码以将新令牌存储在您的数据库中。

    if($_googleClient->isAccessTokenExpired()) {
        // Don't think this is required for Analytics API V3
        //$_googleClient->refreshToken($_analytics->dbRefreshToken($_agencyId));
        echo 'Access Token Expired'; // Debug
    
        $_googleClient->authenticate();
        $NewAccessToken = json_decode($_googleClient->getAccessToken());
        $_googleClient->refreshToken($NewAccessToken->refresh_token);
    }
    

    【讨论】:

    • 修复了它,另外我试图使用访问令牌刷新令牌,而不是使用刷新令牌作为它的参数。 #fail - 谢谢!
    • 你们谁知道我可以如何强制访问令牌过期以使用刷新令牌功能进行测试以获取新的访问令牌? @mpark
    • @Anagio 您应该能够运行 $_googleClient-&gt;refreshToken($NewAccessToken-&gt;refresh_token); 来强制使用新令牌。然后只需将旧令牌与新令牌进行比较,看看它是否改变了。
    • 我知道Missing argument 1 for Google_Client::authenticate(), 有什么想法吗?
    • Mark & RafaSashi :这个答案当时是有效的,看起来 SDK 已经更新,所以这不再有效。 developers.google.com/api-client-library/php/guide/migration
    【解决方案2】:

    使用 try/catch 并使用 catch 重定向/刷新访问令牌。 以下是我用于类似问题的解决方案:

    $plus = new Google_Service_Plus($client);
    try {
    $me = $plus->people->get('me');
    } catch(Exception $e){
            if(!(strpos($_SERVER["REQUEST_URI"],'logout'))){
             if (isset($authUrl)){
                    $redirect = $authUrl;
            }
            else{
                      $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] .'?logout';
            }
            header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
            exit;
    }
    

    你可以用抛出异常的那行替换try下的语句,我猜应该是

    $_googleClient->setClientId($_analytics->gaClientId);
    

    或者您也可以按照此处给出的解决方案尝试刷新令牌:

    https://stackoverflow.com/a/22096740/1675384

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-26
      • 2015-07-19
      • 2014-09-13
      • 2020-07-12
      • 2016-08-13
      • 2016-09-28
      • 2015-11-26
      相关资源
      最近更新 更多