Nodemailer whit GMail SMTP OAuth2 autentication is getting me 'Invalid status code 401'

随声附和 提交于 2019-12-11 05:49:06

问题


I'm trying to make a simple mail sender using Nodemailer 3, the GMail API whit OAuth2 autentication.

Here's my script:

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);
      })
  }


}

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

回答1:


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



来源:https://stackoverflow.com/questions/42722201/nodemailer-whit-gmail-smtp-oauth2-autentication-is-getting-me-invalid-status-co

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!