1

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' }
  • i make a [copy of this question in Stack Overflow español](http://es.stackoverflow.com/questions/54470/nodemailer-con-gmail-smtp-y-autenticaci%C3%B3n-oauth2-me-devuelve-invalid-status-cod?noredirect=1#comment97072_54470) – Santiago Barchetta Mar 10 '17 at 17:46
  • 1
    Did you figure this out? Having the same problem. I also don't know how you get the `refreshToken`. Isn't that sent in the response? – ironicaldiction Mar 31 '17 at 21:50
  • Not really. I decide to use the simple SMPT authentication. – Santiago Barchetta Apr 03 '17 at 17:17
  • 1
    Ah, I see. I got it to work with OAuth2 after banging my head against a wall for hours: See my answer here http://stackoverflow.com/questions/26196467/sending-email-via-node-js-using-nodemailer-is-not-working/43202668#43202668 – ironicaldiction Apr 04 '17 at 09:06

2 Answers2

2

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

Yohan Blake
  • 1,298
  • 4
  • 21
  • 43
1

I was able to fix this by going to https://console.cloud.google.com/apis/credentials, selecting the correct OAuth 2.0 Client ID, and choosing "Reset Secret" at the top. This revoked my old secret and generated a new one, which I then used in the OAuth Playground to exchange for a refresh token. I put the new Client Secret and Refresh Token in the auth object for nodemailer and it started working again.

Hank
  • 56
  • 2