Gmail身份验证令牌在一小时内到期

问题描述

Gmail身份验证令牌会在一小时后过期,是否可以将令牌的生存期延长至24小时或更长时间? 我正在使用以下方法

const oauth2client = await new google.auth.OAuth2(client_id,client_secret,redirect);
// got the token
oauth2client.setCredentials(tokens);

解决方法

由于您有刷新令牌,因此无需增加令牌TTL

首先查看Gmail API示例之一,并查看刷新令牌会发生什么。

令牌到期时间由所使用的Google API给出,这就是为什么您需要刷新令牌的原因。按照Google Identity Platform DocumentationAccess 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.

不必要延长令牌的寿命,在不再需要时撤销令牌是一个好习惯。只需确保遵循确保应用程序安全的policies

请记住,refresh token might stop working出于以下原因之一:

  • 用户已撤消您应用的访问权限。
  • 刷新令牌已经六个月没有使用了。
  • 用户更改了密码,刷新令牌包含Gmail范围。
  • 用户帐户已超过已授予的(实时)刷新令牌的最大数量。

参考

Using OAuth 2.0 to Access Google APIs

OAuth 2.0 Policies

,

它应该过期了,您只需要使用刷新令牌对其进行刷新即可。