【发布时间】:2021-11-07 12:00:40
【问题描述】:
我们将 googleapis 用于我们的网络应用程序,并存储和管理访问令牌和刷新令牌到我们的数据库。 当我们使用刷新令牌刷新用户的访问令牌时,我们很少遇到 GaxiosError: invalid_grant。
现在,我们使用 google api nodejs 客户端 [ https://github.com/googleapis/google-api-nodejs-client ]。 我们为每个用户将访问令牌和刷新令牌存储到我们的数据库中,并且每 6 小时通过以下逻辑更新它们。
import { google } from 'googleapis';
// create oauth2client for google apis
const oAuth2Client = new google.auth.OAuth2(client_secret, client_id, redirect_uri);
// set current access token and refresh token (==current_tokens)
oAuth2Client.setCredentials(current_tokens);
// refresh access token and refresh token
// new_token contains access_token and refresh_token
const new_token = await oAuth2Client.refreshAccessToken();
// store the new access token and new refresh token to database
...
有谁知道可能导致 GaxiosError: invalid_grant 的原因吗? 我感觉这可能是由于刷新令牌每 6 小时更新一次。
其他信息
生成auth url的设置
const authUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: GOOGLE_APIS_SCOPE, // GOOGLE_APIS_SCOPE contains scopes
prompt: 'consent',
});
【问题讨论】:
标签: node.js oauth google-api google-oauth google-api-nodejs-client