【发布时间】:2018-02-24 22:26:14
【问题描述】:
使用PHP Quickstart代码,发现一个问题:需要刷新token时,代码返回如下错误:
致命错误:未捕获的LogicException:必须传入刷新令牌 或设置为 setAccessToken 的一部分 /app/vendor/google/apiclient/src/Google/Client.php:258
堆栈跟踪:
#0 /app/gmail.php(32): Google_Client->fetchAccessTokenWithRefreshToken(NULL)
#1 /app/test.php(14): getClient()
#2 {main} 投入 /app/vendor/google/apiclient/src/Google/Client.php 第 258 行
我像这样修改了getClient() 函数:
function getClient() {
$client = new Google_Client();
$client->setApplicationName(APPLICATION_NAME);
$client->setScopes(SCOPES);
$client->setAuthConfig(CLIENT_SECRET_PATH);
$client->setRedirectUri(REDIRECT_URL);
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
// Load previously authorized credentials from a file.
$credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
if (file_exists($credentialsPath)) {
$accessToken = json_decode(file_get_contents($credentialsPath), true);
}
else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
return printf("<a href='%s' target='_blank'>auth</a><br />", $authUrl);
}
$client->setAccessToken($accessToken);
// Refresh the token if it's expired.
// ERROR HERE !!
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
}
return $client;
}
第一次认证后(使用getClient()函数中创建的链接),当用户登陆REDIRECT_URL时,会执行callbackAuth()函数:
function callbackAuth() {
$client = new Google_Client();
$client->setApplicationName(APPLICATION_NAME);
$client->setScopes(SCOPES);
$client->setAuthConfig(CLIENT_SECRET_PATH);
$client->setRedirectUri(REDIRECT_URL);
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
// Load previously authorized credentials from a file.
$credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
// Request authorization from the user.
$authCode = trim($_GET['code']);
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
// Store the credentials to disk.
if(!file_exists(dirname($credentialsPath))) {
mkdir(dirname($credentialsPath), 0700, true);
}
file_put_contents($credentialsPath, json_encode($accessToken));
printf("Credentials saved to %s\n", $credentialsPath);
$client->setAccessToken($accessToken);
return $client;
}
我尝试应用其他相关stackoverflow question 的解决方案,但没有结果。为什么会出现这个错误?
【问题讨论】:
-
您是否确认“从文件加载先前授权的凭据”的令牌。有刷新令牌,密钥是
refresh_token?该错误表明它没有。 -
我检查了令牌,它只包含
access_token、token_type、expires_in和created属性。如何检索refresh_token属性? -
错误没有说。请参阅文档。您如何获得 access_token?
-
我使用
callbackAuth()函数获取 access_token。问题已更新以获取更多详细信息。
标签: php google-api access-token google-api-php-client