Gmail 授权令牌将在一小时内过期

Gmail auth token expires within an hour

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