使用PHP Quickstart代码时,我发现了一个问题:当需要刷新内标识时,代码将返回以下错误:
致命错误:未捕获的逻辑异常:刷新令牌必须在/app/vendor/google/apiclient/src/Google/Client.php中传递或设置为setAccessToken的一部分
堆栈追踪:
(32)关于我的博客:Google_客户端-〉使用刷新令牌获取访问令牌(空)
/app/test.php(第14页):请输入密码获取客户端()
在/app/vendor/google/apiclient/src/google/Client. php的第258行中抛出了第2个错误
我将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的解决方案,但没有结果。为什么会出现这种错误?
1条答案
按热度按时间oymdgrw71#
多亏了Alex Blex,我才能注意到第一次收到令牌时有
refresh_token
,但在第一次请求后,refresh_token
没有被存储。解决方案如下(from this answer):refresh_token只在第一次请求时返回。当您第二次刷新访问令牌时,它将返回除refresh_token之外的所有内容,并且file_put_contents将在第二次发生这种情况时删除refresh_token。
按如下方式修改代码会将原始的访问令牌与新的令牌合并。这样您就可以保留refresh_token以备将来请求使用。