为什么OAuth2与Gmail Nodejs Nodemailer产生“用户名和密码不被接受”错误 [英] Why is OAuth2 with Gmail Nodejs Nodemailer producing "Username and Password not accepted" error

查看:164
本文介绍了为什么OAuth2与Gmail Nodejs Nodemailer产生“用户名和密码不被接受”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试使用Gmail + Nodejs + Nodemailer发送电子邮件时,OAuth2正在生成用户名和密码不被接受错误


$ b 代码 - Nodejs - Nodemailer和xoauth2

  var nodemailer = require(nodemailer); 
$ b $ var generator = require('xoauth2')。createXOAuth2Generator({
user:,// Your gmail address。

clientId:,
clientSecret:,
refreshToken:,
});



//监听令牌更新
//你可能想把这些存储到一个db
generator.on('token',function (令牌){
console.log('%s的新令牌:%s',token.user,token.accessToken);
});


// login
var smtpTransport = nodemailer.createTransport({
service:'gmail',
auth:{
xoauth2:发电机
}
});


var mailOptions = {
to:,
主题:'Hello',//主题行
文本:'Hello world', // plaintext body
html:'< b> Hello world< / b>'// html body
};


smtpTransport.sendMail(mailOptions,function(error,info){
if(error){
console.log(error);
}其他{
console.log('Message sent:'+ info.response);
}
smtpTransport.close();
});

问题:


  • 我使用Google OAuth2操场创建了令牌, https://developers.google.com/ oauthplayground /


  • 它使用refreshToken获取一个有效的accessToken ok(即它在屏幕上打印新的访问令牌)。在尝试发送电子邮件之前没有错误。

  • 我添加了可选的accessToken:但得到了同样的错误。 (用户名和密码不被接受)


  • 我不是100%确定用户名,文档说它需要一个用户 - 我猜想为标记创建的帐户的电子邮件,但不是100%清楚。我已经尝试了几件事情,但都没有成功。

  • 我已经搜索了Gmail账户上的选项,没有发现任何看起来不对的内容。


  • 另外,当我使用Java进行此操作时,它需要google用户标识而不是电子邮件地址,不知道为什么这是使用电子邮件地址,而Java使用的是UserId。

  • b
    $ b

    问题在于范围



    不符合:
    https://www.googleapis.com/auth/gmail.compose



    但如果我使用
    https://mail.google.com/


    OAuth2 is producing "Username and Password not accepted" error when try to send email with Gmail+ Nodejs+Nodemailer

    Code - Nodejs - Nodemailer and xoauth2

    var nodemailer = require("nodemailer");
    
    var generator = require('xoauth2').createXOAuth2Generator({
        user: "", // Your gmail address.
    
        clientId: "",
        clientSecret: "",
        refreshToken: "",
    });
    
    
    
    // listen for token updates
    // you probably want to store these to a db
    generator.on('token', function(token){
        console.log('New token for %s: %s', token.user, token.accessToken);
    });
    
    
    // login
    var smtpTransport = nodemailer.createTransport({
        service: 'gmail',
        auth: {
            xoauth2: generator
        }
    });
    
    
    var mailOptions = {
        to: "",
        subject: 'Hello ', // Subject line
        text: 'Hello world ', // plaintext body
        html: '<b>Hello world </b>' // html body
    };
    
    
    smtpTransport.sendMail(mailOptions, function(error, info) {
      if (error) {
        console.log(error);
      } else {
        console.log('Message sent: ' + info.response);
      }
      smtpTransport.close();
    });
    

    issues:

    • I used Google OAuth2 playground to create the tokens, https://developers.google.com/oauthplayground/

    • It looks to grab a valid accessToken ok, using the refreshToken, (i.e. it prints the new access token on the screen.) No errors until it tries to send the email.

    • I added the optional accessToken: but got the same error. ( "Username and Password not accepted")

    • I am not 100% sure about the "username", the docs say it needs a "user" email address - I guess the email of the account that created to token, but is not 100% clear. I have tried several things and none worked.

    • I have searched the options on the gmail accounts, did not find anything that looks wrong.

    • Also, when I did this with Java, it needed the google userID rather than the email address, not sure why this is using the email address and the Java is using the UserId.

    解决方案

    nodemailer fails with a "compose" scope

    The problem was the "scope"

    it fails with: https://www.googleapis.com/auth/gmail.compose

    but works ok if I use https://mail.google.com/

    这篇关于为什么OAuth2与Gmail Nodejs Nodemailer产生“用户名和密码不被接受”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆