Nodemailer的GMail SMTP OAuth2自动认证让我“无效状态码401” [英] Nodemailer whit GMail SMTP OAuth2 autentication is getting me 'Invalid status code 401'

查看:167
本文介绍了Nodemailer的GMail SMTP OAuth2自动认证让我“无效状态码401”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Nodemailer 3(GMail API的OAuth2自动化)创建一个简单的邮件发件人。

I'm trying to make a simple mail sender using Nodemailer 3, the 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_token时,电子邮件发送没有任何问题。但是,当我未指定 access_token 时,只有de refresh_token 时,出现此错误:

When I force the access_token in the auth object, the email is sent without any problem. But, when I don't specify the access_token, only de refresh_token, I'm getting this error:

{ 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' }


推荐答案

也许是为时已晚,但只是在这里回答,以防有人遇到相同的问题。重置refreshToken时,我的问题得以纠正。 本文给出了整个过程的好指导

Maybe it's too late, but just answering here incase someone comes across the same issue. My problem rectified when the refreshToken was reset. This article gives great instructions for the whole process

这篇关于Nodemailer的GMail SMTP OAuth2自动认证让我“无效状态码401”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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