代码之家  ›  专栏  ›  技术社区  ›  user1063287

在OAuth和Nodemailer中使用Gmail的确切方法是什么?

  •  1
  • user1063287  · 技术社区  · 8 年前

    使用 Gmail , OAuth2 Nodemailer 从服务器端发送电子邮件 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

    我发布这个解决方案是为了确认这是最佳实践,如果是的话,也是为了节省其他人的时间。

    2 回复  |  直到 8 年前
        1
  •  10
  •   user1063287    7 年前

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

    app.js

    02年) Google OAuth2


    应用程序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) 如果没有项目,系统将提示您创建一个

    enter image description here

    03) Create Project

    04) 点击 Create

    enter image description here
    05) 输入a Project Name 创建

    enter image description here

    06) 选择 Gmail API

    enter image description here

    07) 点击 Enable

    enter image description here

    08) 点击 Create Credentials

    enter image description here

    09)

    enter image description here

    10) 为OAuth客户机命名并确保添加 https://developers.google.com/oauthplayground redirect URI 为了生成 refresh access

    enter image description here
    11) 定义同意屏幕设置

    enter image description here

    12) I'll do this later Done

    enter image description here

    13) 点击 Edit 图标,查看 Client ID Client Secret

    enter image description here

    14) 产生 接近 代币,转到 https://developers.google.com/oauthplayground

    15) cog 右上角的图标,检查 Use your own OAuth credentials 然后进入 客户端ID 客户机密

    enter image description here

    16) 在左栏中,选择 Gmail API v1 Authorise APIs

    enter image description here

    17) 如果您已登录多个帐户,则在提示时选择相关帐户

    enter image description here

    18) 点击 Allow

    enter image description here

    19) 点击 Exchange authorisation code for tokens

    enter image description here
    我不知道为什么会有倒计时 接近 令牌,但希望屏幕底部的消息意味着令牌不会过期。

        2
  •  2
  •   Gono    7 年前

    OAuth同意屏幕

    不过,我认为值得一提的是,在凭证页面中还有另一个步骤: OAuth同意屏幕 标签。

    敏感范围 在被要求提交之前。

    配额呢?

    应用程序主页链接 授权域 如果你在一个后台应用程序上工作的话,这是你可能没有的。

    推荐文章