【问题标题】:What is the definitive way to use Gmail with OAuth and Nodemailer?将 Gmail 与 OAuth 和 Nodemailer 一起使用的最终方法是什么?
【发布时间】:2019-01-26 18:02:53
【问题描述】:

期望的行为

使用GmailOAuth2Nodemailer 从服务器端node.js 文件发送电子邮件。

我的尝试

相关文档

https://nodemailer.com/smtp/oauth2
https://nodemailer.com/usage/using-gmail
https://developers.google.com/gmail/api/auth/web-server

相关问题

send emails from MY gmail account with OAuth2 and nodemailer
How do I authorise an app (web or installed) without user intervention?
https://stackoverflow.com/a/47936349
https://stackoverflow.com/a/22572776

上述来源的说明存在差距,并且某些信息已过时,因此下面的答案是我的最终实现,它似乎正在运行。

我发布此解决方案是为了确认这是最佳做法,如果是的话,可以节省其他人的时间。

【问题讨论】:

    标签: node.js oauth-2.0 nodemailer


    【解决方案1】:

    以下对我有用,有两个部分:

    01) app.js

    02) GoogleOAuth2 设置


    app.js

    var nodemailer = require("nodemailer");
    
    var transporter = nodemailer.createTransport({
        host: 'smtp.gmail.com',
        port: 465,
        secure: true,
        auth: {
            type: 'OAuth2',
            user: local_settings.my_gmail_username,
            clientId: local_settings.my_oauth_client_id,
            clientSecret: local_settings.my_oauth_client_secret,
            refreshToken: local_settings.my_oauth_refresh_token,
            accessToken: local_settings.my_oauth_access_token
        }
    });
    
    
    var mail = {
        from: "John Smith <me@mydomain.com>",
        to: "user@userdomain.com",
        subject: "Registration successful",
        text: "You successfully registered an account at www.mydomain.com",
        html: "<p>You successfully registered an account at www.mydomain.com</p>"
    }
    
    transporter.sendMail(mail, function(err, info) {
        if (err) {
            console.log(err);
        } else {
            // see https://nodemailer.com/usage
            console.log("info.messageId: " + info.messageId);
            console.log("info.envelope: " + info.envelope);
            console.log("info.accepted: " + info.accepted);
            console.log("info.rejected: " + info.rejected);
            console.log("info.pending: " + info.pending);
            console.log("info.response: " + info.response);
        }
        transporter.close();
    });
    

    Google 和 OAuth 设置

    上面的代码需要如下设置:

    01)转到https://console.developers.google.com

    02)如果没有项目,会提示创建一个

    03)点击Create Project

    04)点击Create


    05) 输入Project Name 并点击Create

    06) 选择Gmail API

    07)点击Enable

    08)点击Create Credentials

    09)输入需要的设置

    10) 为 OAuth 客户端命名并确保将 https://developers.google.com/oauthplayground 添加为 redirect URI 以便稍后生成 refreshaccess 令牌


    11) 定义同意屏幕设置

    12)点击I'll do this laterDone

    13) 点击Edit 图标,查看您的Client IDClient Secret

    14) 要生成accessrefresh 令牌,请转到https://developers.google.com/oauthplayground

    15)点击右上角cog图标,勾选Use your own OAuth credentials并输入Client IDClient Secret

    16)在左栏中选择Gmail API v1并点击Authorise APIs

    17)如果您登录了多个帐户,当提示时选择相关帐户

    18)点击Allow

    19)点击Exchange authorisation code for tokens


    我不确定为什么access 令牌会倒计时,但希望屏幕底部的消息意味着令牌不会过期。

    【讨论】:

    • 你拯救了我的一天!!!这个答案值得更多的投票。留下最佳答案!!!
    • 我希望我能给你 1000 个赞,这是一个巨大的帮助
    • 这太有帮助了!谢谢您的发布。很好的指导,完全适合我。
    • @brillout - 一些人在 cmets 中提到了这一点,我只能根据我在帖子中实施它的经验来发言,但是在发布解决方案两年后它仍然对我有用,并且我不必重新创建任何令牌。
    • 我知道这已经过时了,但我目前正在经历同样的头痛,而且我已经完成了您展示的所有内容,并且能够发送电子邮件。但是根据 Oauth 文档,如果您正在使用处于测试状态的应用程序,即。不是经过验证的应用程序,刷新令牌每 7 天过期一次,每个帐户最多有 50 个刷新令牌。所以这个解决方案是一种创可贴,因为我只想用它来简单地从我的网站发送一封电子邮件。我不需要验证应用程序即可将我自己的电子邮件发送给我自己。现在我想弄清楚如何使用服务帐户
    【解决方案2】:

    OAuth 同意屏幕

    您对差距和过时信息的看法完全正确,并且您在记录将 Gmail 与 OAuth 和 nodemailer 一起使用所需的步骤方面做得非常出色! 尽管如此,我认为值得一提的是,在凭据页面中还有一个步骤:OAuth 同意屏幕选项卡。

    它包含一个类似于 Google Play 应用程序提交的表单,需要 Google 验证,如果您选择不验证您的应用程序,则您之前有 100 次他们所谓的敏感范围调用的限制被要求提交。

    配额呢?

    我仍然不清楚这 100 个调用配额是否会被消耗,即使您没有选择任何其他权限来使用敏感范围(默认权限是电子邮件、个人资料、openid)。我希望不会,因为 OAuth 同意屏幕要求提供诸如 应用程序主页链接授权域 之类的内容,如果您正在处理后端应用程序,这些内容可能没有。

    我认为整个过程真的很慢而且很复杂,因为大多数人执行所有这些步骤只是为了使用 nodemailer 从他们的应用程序发送一封电子邮件......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-30
      • 2012-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多