在google calendar API中遇到RefreshToken的问题

问题描述 投票:0回答:1

我在从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');。请帮忙解决这个错误。

php google-calendar-api google-api-php-client
1个回答
0
投票

$客户端 - > fetchAccessTokenWithRefreshToken($客户机 - > getRefreshToken());

$ client-> getRefreshToken()应该在被$ client-> fetchAccessTokenWithRefreshToken()使用之前存储在var / object中

$refresh_token = $client->getRefreshToken(); 
$client->fetchAccessTokenWithRefreshToken($refresh_token);
© www.soinside.com 2019 - 2024. All rights reserved.