在Gmail API上使用刷新令牌。跟踪旧密钥

问题描述 投票:1回答:1
  • 我有一个完全正常工作服务器端应用程序,用于与单个用户的Gmail收件箱进行交互。
    • 我拥有API信誉,并且刷新令牌已保存在持久安全存储中。
    • 应用程序可以成功与Gmail进行交互,直到访问令牌到期为止。

这是我面临的问题:

  • 一旦访问令牌过期,我将无法仅使用我的API凭据和刷新令牌来生成新令牌。
  • 如果尝试仅将刷新令牌字符串作为参数来调用invalid_grant,则会收到错误fetchAccessTokenWithRefreshToken()
  • 我可以获得新令牌的唯一方法是提供刷新令牌和当前访问令牌信息!
    • 不仅我需要原始访问令牌本身,而且还需要createdexpires_in值。

我必须向其传递一整套信息:[access_token, expires_in, created, refresh_token],否则它将根本无法工作!

显然我在这里做错了,我想我所需要的只是刷新令牌,以根据需要生成访问令牌。

[PHP]以下是一些摘要:

(请注意,这些仅是开发人员,我不打算对代码中的秘密进行硬编码)

此工作,因为我正在为它提供完整的$token数组:

    $token = array();
    $token['access_token'] = '<<SCRUBBED_CURRENT_ACCESS_TOKEN>>>';
    $token['expires_in'] = 3599;
    $token['refresh_token'] = '<<SCRUBBED_REFRESH_TOKEN>>';
    $token['created'] = 1587447211;

    // If I leave out ANY of the values above, the token refresh does not work! 

    // omitted some Gmail client configuration and setup. 

    $this->client->setAccessToken($token);

    if ($this->client->isAccessTokenExpired()) {
      $this->accessToken =  $this->client->fetchAccessTokenWithRefreshToken($token);
    }
    else {
      $this->accessToken = $this->client->getAccessToken();
    }
    $this->service = new Google_Service_Gmail($this->client);

我希望它像这样工作:

    // Omitted initial Gmail client setup

    if ($this->client->isAccessTokenExpired()) {
      $this->accessToken =  $this->client->fetchAccessTokenWithRefreshToken('<MY_REFRESH_TOKEN');
    }
    else {
      $this->accessToken = $this->client->getAccessToken();
    }
    $this->service = new Google_Service_Gmail($this->client);
php google-api google-oauth gmail-api google-api-php-client
1个回答
0
投票
if ($client->isAccessTokenExpired()) { $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); }

also see Oauth2Authencation.php

© www.soinside.com 2019 - 2024. All rights reserved.