我使用 Gmail API 来读取电子邮件并添加/删除标签。
我的身份验证正确,我获得了具有 3600 秒生命周期的访问令牌。
问题就开始了......当我尝试刷新令牌时,我得到一个空值,所以我无法刷新它。
一些代码:
$this->client = new Client();
$this->client->setApplicationName('App');
$this->client->setClientId('xxxxxxx');
$this->client->setClientSecret('xxxxxxx');
$this->client->setRedirectUri('xxxxxxx/oauth/gmail/callback');
$this->client->setScopes([GoogleGmail::GMAIL_READONLY, GoogleGmail::GMAIL_MODIFY]);
$this->client->setPrompt('select_account consent');
$this->client->setAccessType('offline');
在回调中我这样做:
$this->client->fetchAccessTokenWithAuthCode($code);
输出:
{
"access_token":"xxxxxxxxxx",
"expires_in":3599,
"scope":"https:\/\/www.googleapis.com\/auth\/gmail.readonly https:\/\/www.googleapis.com\/auth\/gmail.modify",
"token_type":"Bearer",
"created":7374384324
}
我确实保存了访问令牌、过期时间和所有数据。但我没有得到刷新令牌。
我尝试:
$this->client->getRefreshToken(); // null
如果我这样做
$this->client->isAccessTokenExpired()
我总是得到
true
,包括如果我在 5 秒前授权的话。
我正在使用
google/apiclient v2.15.0
,PHP 8.1
谢谢
您仅在用户第一次验证您的应用程序时获得刷新令牌。您需要将其与用户名一起存储在您的应用程序中,以便您可以在需要请求新的访问令牌时发送该信息。
在 Web 应用程序中刷新访问令牌不会返回新的刷新令牌。再次请求授权也不会产生新的刷新令牌。