Search code examples
oauth-2.0gmail-apinodemailerfeathersjs

How to work nodemailer smtp oauth2 with gmail api? Got error 535: user and pass not accepted


I enabled Gmail API on google API console dashboard.
I created the credentials choosing OAuth client Id, web applications, and 'https://developers.google.com/oauthplayground' as authorized redirect uri.
I got the refresh token by playing in the OAuth 2.0 playground using the credentials above, and enabling https://mail.google.com/ for scope.

const mailer = require('feathers-mailer');
const smtpTransport = require('nodemailer-smtp-transport');

app.use('/api/mailer', mailer(smtpTransport({
    service: 'gmail',
    host: 'smtp.gmail.com',
    port: 587,
    secure: false,
    requireTLS: true,
    auth: {
      type: 'OAuth2',
      user: app.get('gmail'),
      scope: 'https://mail.google.com/',
      clientId: app.get('gmail_secret').client_id,
      clientSecret: app.get('gmail_secret').client_secret,
      refreshToken: app.get('gmail_credentials').refresh_token
    }
  })));

I got error 535: username and password not accepted, including 'command:auth plain' in case it mattered.

I was sure those app gets gave the correct values because I have replaced the app gets with exact values and it still gave the same error.
I also tried auth object with removed 'host', 'port', 'secure', 'requireTLS'. Same error.
I tried auth: host: 'smtp.gmail.com', port:'465', secure: true. Same error.
On a local environment, in case it will help fixing the error.

How do I fix this 535 error?


Solution

  • Feathers-Mailer included nodemailer and did .createTransport automatically. SMTP transport module was the error and was not needed. mailer({transport-object}) worked.