如何使用存储的刷新令牌获取更新的访问令牌

9
我正在构建一个应用程序,允许管理员通过身份验证访问其分析帐户以进行离线使用,并将刷新令牌存储在数据库中。
现在,当我尝试在前端使用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 的功能,能够将先前在管理员身份验证过程中从数据库中获取的值输入其中进行刷新。
2个回答

7
您可以尝试以下方法,需要添加代码将新token存储在您的数据库中。
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);
}

1
搞定了,而且我一直在尝试使用访问令牌刷新令牌,而不是使用刷新令牌作为参数。#失败 - 谢谢! - mattpark22
1
你们两个知道我怎么强制访问令牌过期以测试使用刷新令牌功能获取新的访问令牌吗?@mpark - Anagio
1
@Anagio,你应该可以运行$_googleClient->refreshToken($NewAccessToken->refresh_token);来强制获取新的令牌。然后只需比较旧令牌和新令牌是否有变化即可。 - Gareth Luckett
4
我收到了“Google_Client::authenticate()缺少参数1”的错误信息,你有什么想法吗? - RafaSashi
你不能在没有授权码的情况下调用 authenticate() -- 为什么这个问题会得到这么多赞和勾选?!? - mpen
1
Mark和RafaSashi:此答案在当时是有效的,但看起来SDK已经更新,因此不再适用。https://developers.google.com/api-client-library/php/guide/migration - Gareth Luckett

0

使用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://dev59.com/OGIj5IYBdhLWcg3wKiLs#22096740


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接