NodeMailer - sending mail with a Google service account is not performed because "Username and password are not accepted"

I am creating a Twitter bot and I am implementing a method that sends me an email if there is an error. Since I already use the Google API to access Google Drive (no problem here), I decided to use the service account to send email (the Google console says that it can be used in this way)

The method I came to to send an email so far:

var config = require('./config/mail');
var google = require('./config/google');
var nodemailer = require('nodemailer');

var send = function (args) {
  let transporter = nodemailer.createTransport({
    'service': 'gmail',
    'auth': {
        'type': 'OAuth2',
        'user': google.client_email,
        'serviceClient': google.client_id,
        'privateKey': google.private_key
    }
  });
  transporter.on('token', token => console.log(token));

  let message = {
    'from': `"${config.serverFromName}" <${config.serverFromMail}>`,
    'to': args.to,
    'subject': args.subject,
    'text': args.text,
    'html': `<p>${args.text}</p>`
  };

  transporter.sendMail(message, (err, info) => {
    if (err) {
      console.log('Mail couldn\'t be sent because: ' + err);
    } else {
      console.log('Mail sent');
    }
  });
};

config/google , Google . config.serverFromName config.serverFromMail - ( ). args

, :

Mail couldn't be sent because: 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 z123sm543690vkd.10 - gsmtp

, , , , :

{ user: 'name@project.iam.gserviceaccount.com',
  accessToken: 'ya29.ElmIBLxzfU_kkuZeyISeuRBeljmAe7HNTlwuG4K12ysUNo46s-eJ8NkMYHQqD_JrqTlH3yheNc2Aopu9B5vw-ivEqvPR4sTDpWBOg3xUU_4XiJEBLno8FHsg',
  expires: 1500151434603 }

, OAuth. , , , . Google.

, ?

+4
1

: , Google , nodemailer. !

! , Google Admin Console . (, ) .

, Node.JS Quickstart API Google .

  • console.developers.google.com, OAuth2.0 client_secret.json .
  • NPM google api.

    npm install googleapis

    npm install google-auth-library

  • quickstart.js

  • client_secret.json quickstart.js

  • 7 quickstart.js - , . , . , . . API Gmail.

  • RUN node quickstart.js

  • URL- , . nodejs-gmail-quickstart.json, stdout.
    , . , SCOPES, access_token .

: access_token 1 . refresh_token .

refresh_token! auth 3LO Nodemailer. , . auth :

const mailbot = nodemailer.createTransport({
      host: 'smtp.gmail.com',
      port: 587,              // TLS (google requires this port for TLS)
      secure: false,          // Not SSL
      requireTLS: true,       // Uses STARTTLS command (nodemailer-ism)
      auth: {
          // **HIGHLY RECOMMEND** ALL values be
          //  read in from a file not placed directly in code.  
          // Make sure that file is locked down to only the server daemon
          type : 'OAuth2',
          user : config.client_email,
          scope : "https://www.googleapis.com/auth/gmail.send",
          clientId : config.client_id,
          clientSecret: secret,
          refreshToken: activeToken.refresh_token

          // AT RUNTIME, it looks like this:
          //type : 'OAuth2',
          //user : 'user@gmail.com',   // actual user being impersonated
          //scope : "", //Optional, but recommend to define for the action intended
          //clientId : '888888888998-9xx9x99xx9x99xx9xxxx9xx9xx9x88x8xxx.apps.googleusercontent.com',
          //clientSecret: 'XxxxxXXxX0xxxxxxxx0XXxX0',
          //refreshToken: '1/XXxXxsss-xxxXXXXXxXxx0XXXxxXXx0x00xxx'              
      }
 });

: Gmail FROM , ( ). , { FROM: '"Display NAME" <user email>' }, , .

. nodemailer https://accounts.google.com/o/oauth2/token , access_token.

, nodemailer , , this.emit(). , , , , access_token .

[] , ! , 2LO , ID ID . , . . . Google Identity Platform (Nodemailer HTTP/REST) ​​

[1] Google OAuth 2.0 https://accounts.google.com/o/oauth2/v2/auth. HTTPS. HTTP- .

[5] , - , .

TLS , refresh_token ( hassle, ), access_token, API Google.

, OAuth2.0 ( ), , , . !

+1

Source: https://habr.com/ru/post/1681533/


All Articles