我在从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');
。请帮忙解决这个错误。
$客户端 - > fetchAccessTokenWithRefreshToken($客户机 - > getRefreshToken());
$ client-> getRefreshToken()应该在被$ client-> fetchAccessTokenWithRefreshToken()使用之前存储在var / object中
$refresh_token = $client->getRefreshToken();
$client->fetchAccessTokenWithRefreshToken($refresh_token);