我应该如何处理刷新令牌(Google 日历)

How should I do with refresh token (Google calendar)

过去几天我浪费了很多时间 现在我设法获得了刷新令牌..我之前无法获得刷新令牌..

我的目的是在没有 Google 用户登录的情况下同步 Google 日历数据

我为此做了什么:

  1. 我在我的数据库中存储了代币信息
  2. 当用户想要同步时,我使用存储的信息从 Google 日历 API 中提取数据 但是令牌有到期日期,即一个小时
  3. 我假设我可以使用刷新令牌获取可用令牌并再次从 Google 日历 api 中提取数据(但如何?)

额外问题:我可以从之前的刷新令牌中获取另一个刷新令牌吗?是不是好像可以一遍又一遍地获取刷新令牌?

这是我的一些代码:

 public function __construct()
{
    $client = new Google_Client();
    $client->setAuthConfig('client_secret.json');
    $client->addScope(Google_Service_Calendar::CALENDAR); // CALENDAR READONLY 는 읽기만 가능
    $guzzleClient = new \GuzzleHttp\Client(array('curl' => array(CURLOPT_SSL_VERIFYPEER => false)));
    $client->setHttpClient($guzzleClient);
    $client->setAccessType ("offline");
    $client->setApprovalPrompt ("force");
    $client->setRedirectUri('http://localhost/smart_mirror/GoogleCalendarApi-master/public/api/cal');
    $this->client = $client;
}


$refresh_token=$this->client->fetchAccessTokenWithRefreshToken($this->client->getRefreshToken()); 
$this->client->getAuth()->refreshToken($this->client->getAuth()->getRefreshToken()); 
$refresh_token=$this->client->getAccessToken();

Access tokens have limited lifetimes. If your application needs access to a Google API beyond the lifetime of a single access token, it can obtain a refresh token. A refresh token allows your application to obtain new access tokens.

Note: Save refresh tokens in secure long-term storage and continue to use them as long as they remain valid. Limits apply to the number of refresh tokens that are issued per client-user combination, and per user across all clients, and these limits are different. If your application requests enough refresh tokens to go over one of the limits, older refresh tokens stop working.

Refreshing an access token (offline access)

如果您的应用程序需要离线访问 Google API,请将 API 客户端的访问类型设置为离线:

$client->setAccessType("offline");

这是一个 附加示例。