我遇到了从google oauth2.0 calendar API获取刷新访问令牌的问题。我的代码是:
function getClient() {
$client = new Google_Client();
$client->setApplicationName(APPLICATION_NAME);
$client->setScopes(SCOPES);
$client->setAuthConfig(CLIENT_SECRET_PATH);
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
$credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
// Load previously authorized credentials from a file.
if (file_exists($credentialsPath)) {
$accessToken = json_decode(file_get_contents($credentialsPath), true);
$client->setAccessToken($accessToken);
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
try {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
}
catch(Exception $e) { die("Error is: ".$e->getMessage()); }
file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
}
$authUrl = '';
return $client;
}
}
我在fetchAccessTokenWithRefreshToken
获得了例外。我在哪里使用try catch。获得例外Error is: refresh token must be passed in or set as part of setAccessToken
。根据github上的线程,我还使用客户端对象传递了$client->setAuthConfig(CLIENT_SECRET_PATH); $client->setAccessType('offline');
。
请帮忙解决这个错误。
答案 0 :(得分:0)
在$ client-> fetchAccessTokenWithRefreshToken()使用之前,$客户端 - > fetchAccessTokenWithRefreshToken($客户端 - > getRefreshToken());
$refresh_token = $client->getRefreshToken();
$client->fetchAccessTokenWithRefreshToken($refresh_token);