javascript - 为什么我在生成 token 时收到错误 'Can' ?使用 Nodemailer 时检查您的身份验证选项?

标签 javascript nodemailer

我看到几个关于正常誓言的类似问题。然而,我决定尝试新的nodemailers 支持 Gmail 服务帐户的功能。我一遍又一遍地遇到同样的错误,但没有说明如何解决它。有没有人有任何见解?

第 1 步:我从项目中设置了服务帐户。然后我下载了 key.json 文件。

第 2 步:我转到 GCP API 并为我的项目启用了 gmail api。然后我验证了我的新服务帐户是否在列表中。 (我不想发布图片,因为它包含敏感信息。但我三次检查它是否位于为 gmail api 启用的服务帐户列表中。

第 3 步:我编写了一些代码。

return Promise.resolve()
.then(() => {
      const mailTransport = nodemailer.createTransport({
        service: 'gmail',
        auth: {
          type: 'OAuth2',
          user: <service account email>,
          serviceClient: <service account client>,
          privateKey: <Private key> (including the \n at the end),
        },
      });
})
.then(() => {
        const mailOptions = {
            from: '"Support" <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b9caccc9c9d6cbcdf9d4c0d8c9c997dad6d4" rel="noreferrer noopener nofollow">[email protected]</a>',
            to: targetEmail,
            subject: 'My Subject',
            html: 'My super support email'
          };
          return mailTransport.sendMail(mailOptions);
    })
.catch(err => {
  console.error(err);
})

我打印了一个很棒的错误,上面写着

Error { Error: Can't generate token. Check your auth options
at SMTPConnection._handleXOauth2Token (/workspace/node_modules/nodemailer/lib/smtp-connection/index.js:1697:27)
at SMTPConnection.login (/workspace/node_modules/nodemailer/lib/smtp-connection/index.js:540:22)
at XOAuth2.generateToken (/workspace/node_modules/nodemailer/lib/xoauth2/index.js:170:33)
at XOAuth2.getToken (/workspace/node_modules/nodemailer/lib/xoauth2/index.js:123:18)
at connection.connect (/workspace/node_modules/nodemailer/lib/smtp-transport/index.js:374:32)
at SMTPConnection.once (/workspace/node_modules/nodemailer/lib/smtp-connection/index.js:215:17)
at Object.onceWrapper (events.js:286:20)
at SMTPConnection.emit (events.js:198:13)
at SMTPConnection.EventEmitter.emit (domain.js:466:23)
at SMTPConnection._actionEHLO (/workspace/node_modules/nodemailer/lib/smtp-connection/index.js:1313:14) code: 'EAUTH', command: 'AUTH XOAUTH2' }

有人知道我做错了什么吗?

注意:了解更多背景信息。它在 firebase 函数内部运行。

最佳答案

我刚刚遇到了同样的问题,我在这里找到了解决方案:

https://levelup.gitconnected.com/multi-purposes-mailing-api-using-nodemailer-gmail-google-oauth-28de49118d77

如教程中所述,您必须获取刷新 token 。

在 Google API 控制台中的 OAuth 凭据中提供 https://developers.google.com/oauthplayground/作为重定向 URI。

转到https://developers.google.com/oauthplayground/页。

在设置菜单的右上角选择“使用您自己的 OAuth 凭据”并提供您的凭据。

然后提供链接https://mail.google.com在“选择和授权 API”部分中,单击“授权 API”按钮。

最后用授权码换取 token 。

当您拥有刷新 token 时,您可以在配置中传递它:

{
    service: 'gmail',
    auth: {
        type: 'OAuth2',
        user: USER_EMAIL_ADDRESS,
        clientId: CLIENT_ID,
        clientSecret: CLIENT_SECRET,
        refreshToken: REFRESH_TOKEN,
    },
}

根据这个答案,谷歌的刷新 token 不会过期:

Do Google refresh tokens expire?

最诚挚的问候,

皮奥特尔

关于javascript - 为什么我在生成 token 时收到错误 'Can' ?使用 Nodemailer 时检查您的身份验证选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62987543/

相关文章:

php - 下载脚本非常慢

javascript - 图像裁剪器不适用于外部图像

firebase - 设置环境变量以 "-bash: !": event not found"结尾

node.js - nodemailer 附件不起作用

javascript - Nodejs Node 邮件程序错误

javascript - Shopify + Mixpanel 集成

javascript - 获取文档中当前 div 后具有相同类的下一个 div

javascript - 限制html中的点击按钮

node.js - 使用 nodemailer 发送邮件 - 来自字段的电子邮件不正确

node.js - 如何使用nodemailer和smtp传输发送邮件?