如何使用 gmail api 创建一个新的 token.json 文件?

How to create a new token.json file using gmail api?

我正在处理 Gmail API。我有一个 token.json 文件,其中有一个访问令牌和刷新令牌。但是这些值会过期。 当它到期时,我需要 运行 申请,他们会向我提供 link 需要我的 Gmail 帐户的许可。在获得许可后,他们将提供一个密钥,我们必须将其放置在我们的终端中以获取新令牌。但是如何在不手动执行此操作的情况下创建这个新的令牌文件? 有什么方法可以动态创建这个 token.json 文件吗?

我了解到您有 运行 Gmail API 的 quickstart 示例,并且您想创建一个新的 token.json 而不等待过期时间。为了实现这一点,您可以按以下方式修改函数authorize

function authorize(credentials, callback) {
  const {client_secret, client_id, redirect_uris} = credentials.installed;
  const oAuth2Client = new google.auth.OAuth2(
      client_id, client_secret, redirect_uris[0]);

  return getNewToken(oAuth2Client, callback);

}

更新

token.json 将过期。我了解到您想在每个 运行 中生成一个新的。如果你希望它不会过期,我建议你阅读Quickstartnotes

The authorization flow in this example is designed for a command line application. For information on how to perform authorization in other contexts, see the Authorizing and Authenticating. section of the library's README.

在这里您可以找到不同的方法来验证 Google APIs,满足您的要求。我建议你阅读 OAuth2 and Service account credentials