Nodemailer and GMail OAuth: Username and Password not accepted

Viewed 2351

So i have the following codes:

const nodemailer = require("nodemailer");

const mailer = {
    service: "gmail",
    auth: {
        type: "OAuth2",
        user: "mailtester123123123@gmail.com",
        clientId: "123123123123123123123123123123.apps.googleusercontent.com",
        clientSecret: "krW123123123123123123RqEnS",
        refreshToken: "1/SM-7f31231231231231231231231231231231231231231231231UzH",
        accessToken: "ya29.1234568791231324564897894231324564697897ss3Ggi-TkWO4HSIZBqHiLpoIkT7H_Sgii4RQxyVJ0QjIfjKB5zk"
    }
}

const transporter = nodemailer.createTransport(mailer);
transporter.sendMail({
    to: "mailtester123123123@windowslive.com",
    subject: "test",
    text: "test"
});

But when i try to send email, it always give error as follow: Error: Invalid login: 535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8 https://support.google.com/mail/?p=BadCredentials f67-v6sm75536287pfe.75 - gsmtp

This is the full trace:

(node:25324) UnhandledPromiseRejectionWarning: Error: Invalid login: 535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8  https://support.google.com/mail/?p=BadCredentials f67-v6sm75536287pfe.75 - gsmtp
    at SMTPConnection._formatError (D:\Sources\NodeJS\test\node_modules\nodemailer\lib\smtp-connection\index.js:606:19)
    at SMTPConnection._actionAUTHComplete (D:\Sources\NodeJS\test\node_modules\nodemailer\lib\smtp-connection\index.js:1335:34)
    at SMTPConnection._responseActions.push.str (D:\Sources\NodeJS\test\node_modules\nodemailer\lib\smtp-connection\index.js:1314:26)
    at SMTPConnection._processResponse (D:\Sources\NodeJS\test\node_modules\nodemailer\lib\smtp-connection\index.js:762:20)
    at SMTPConnection._onData (D:\Sources\NodeJS\test\node_modules\nodemailer\lib\smtp-connection\index.js:558:14)
    at TLSSocket._socket.on.chunk (D:\Sources\NodeJS\test\node_modules\nodemailer\lib\smtp-connection\index.js:510:47)
    at TLSSocket.emit (events.js:182:13)
    at addChunk (_stream_readable.js:283:12)
    at readableAddChunk (_stream_readable.js:264:11)
    at TLSSocket.Readable.push (_stream_readable.js:219:10)

Any idea what happened and how to fix this? Thank you.

2 Answers

I had this error and my mistake was that I was trying to give only this permission:

"https://www.googleapis.com/auth/gmail.send"

I fixed it by giving the full permissions:

"https://mail.google.com/"

I'm talking about the scopes used to generate the tokens on https://developers.google.com/oauthplayground/

Related