Google API一旦请求刷新令牌就不会返回刷新令牌

时间:2016-09-24 08:49:29

标签: php oauth-2.0 google-api

我最近使用谷歌API工作了很多。我在获取刷新令牌时遇到了一些麻烦。所以我用Google搜索了一下,找到了“setAccessType('offline')”和“setApprovalPrompt('force')”。它确实返回了刷新令牌,所以我非常兴奋。但是2小时后,我检查了它一直在做什么,我看到我没有刷新令牌,显然当我用刷新令牌获取访问令牌时,它返回一个访问令牌和到期的东西,但它没有返回

$client = new Google_Client();
$client->setApplicationName(APPLICATION_NAME);
$client->setScopes(SCOPES);
$client->setAuthConfig(CLIENT_SECRET_PATH);
$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 if (isset($_POST['authcode'])) {
    $authCode = $_POST['authcode'];

    // 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));
} else {
    // Request authorization from the user.
    $authUrl = $client->createAuthUrl();

    /* ... Code to handle the Auth Code */
}
$client->setAccessToken($accessToken);

// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
    $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
    file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
}

还有其他问题吗?可以自由提问

0 个答案:

没有答案