Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 带GMail SMTP OAuth2自动认证的Nodemailer让我';无效的状态代码401';_Node.js_Email_Oauth 2.0_Google Oauth_Nodemailer - Fatal编程技术网

Node.js 带GMail SMTP OAuth2自动认证的Nodemailer让我';无效的状态代码401';

Node.js 带GMail SMTP OAuth2自动认证的Nodemailer让我';无效的状态代码401';,node.js,email,oauth-2.0,google-oauth,nodemailer,Node.js,Email,Oauth 2.0,Google Oauth,Nodemailer,我正在尝试使用NodEmailer3,GMail API whit OAuth2 autentication创建一个简单的邮件发送者 这是我的剧本: var serverConfig = { gmail: { client_user : 'my@email.com', client_id : '349...myClientId', secret : 'mysecret..123jd123', refresh_token : 'x/xxxxxx

我正在尝试使用NodEmailer3,GMail API whit OAuth2 autentication创建一个简单的邮件发送者

这是我的剧本:

var serverConfig  = {
  gmail: {
      client_user : 'my@email.com',
      client_id : '349...myClientId',
      secret : 'mysecret..123jd123',
      refresh_token : 'x/xxxxxxxxxxxxxx-reZuEMeSuJaSERmCVY',
      access_token : 'xxxxxxxxxxxxxxxxx',
      expires: '3599'
  }
}

// 3LO authentication https://nodemailer.com/smtp/oauth2/#example-3
var transporter = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    type: 'OAuth2',
    user: serverConfig.gmail.client_user,
    clientId: serverConfig.gmail.client_id,
    clientSecret: serverConfig.gmail.secret,
    refreshToken: serverConfig.gmail.refresh_token
  },
});

module.exports = {
    "send": function(_from,_to,_subject,_html,_text){
      // setup email data with unicode symbols
      var mailOptions = {
          from: _from,
          to: _to, // list of receivers
          subject: _subject, // Subject line
          html: _html, // html body
          text: _text // plain text body
      };

      transporter.sendMail(mailOptions, function(error, info){
          if (error) {
              return console.log(error);
          }
          console.log('Message ' + info.messageId + ' sent: %s' + info.response);
      })
  }


}
当我在auth对象中强制使用access_令牌时,电子邮件发送没有任何问题。但是,如果我没有指定
访问\u令牌
,而只指定de
刷新\u令牌
,则会出现以下错误:

{ Error: Invalid status code 401
    at ClientRequest.req.on.res (/myproject/node_modules/nodemailer/lib/fetch/index.js:193:23)
    at emitOne (events.js:96:13)
    at ClientRequest.emit (events.js:188:7)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:474:21)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:99:23)
    at TLSSocket.socketOnData (_http_client.js:363:20)
    at emitOne (events.js:96:13)
    at TLSSocket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at TLSSocket.Readable.push (_stream_readable.js:134:10)
  type: 'FETCH',
  sourceUrl: 'https://accounts.google.com/o/oauth2/token',
  code: 'EAUTH',
  command: 'AUTH XOAUTH2' }

也许已经太晚了,但如果有人遇到同样的问题,就在这里回答吧。重置刷新令牌后,我的问题得到纠正。为整个过程提供了很好的指导

我可以通过转到,选择正确的OAuth 2.0客户端ID,并在顶部选择“Reset Secret”来解决这个问题。这撤销了我的旧秘密并生成了一个新的秘密,然后我在OAuth游乐场中使用它来交换刷新令牌。我将新的客户端密码和刷新令牌放在nodemailer的auth对象中,它又开始工作了。

我做了一个测试,你发现了吗?有同样的问题。我也不知道您是如何获得刷新令牌的。这不是在回复中发送的吗?不是真的。我决定使用简单的SMPT身份验证。啊,我明白了。在我的头撞到墙上几个小时后,我用OAuth2实现了这一点:见我的答案这里的文章Goe:(如果可能的话,你能重新发布Articule链接吗?对不起,如果那篇文章已经删除了作者。希望这两个链接能帮到你。)