Home > OS >  Google OAuth2.0 - Gaxios invalid_grant error
Google OAuth2.0 - Gaxios invalid_grant error

Time:06-17

The below is my mailer.js code

    const nodemailer = require("nodemailer");
const { google } = require("googleapis");
const { OAuth2 } = google.auth;
const oauth_link = "https://developers.google.com/oauthplayground";
const { EMAIL, MAILING_ID, MAILING_REFRESH, MAILING_SECRET } = process.env;

const auth = new OAuth2(
  MAILING_ID,
  MAILING_SECRET,
  MAILING_REFRESH,
  oauth_link
);

exports.sendVerificationEmail = (email, name, url) => {
  console.log("inside send verification email");
  auth.setCredentials({
    refresh_token: MAILING_REFRESH,
  });

  const accessToken = auth.getAccessToken();
  console.log("accessToken-"   accessToken);

  const smtp = nodemailer.createTransport({
    service: "gmail",
    auth: {
      type: "OAuth2",
      user: EMAIL,
      clientId: MAILING_ID,
      clientSecret: MAILING_SECRET,
      refreshToken: MAILING_REFRESH,
      accessToken,
    },
  });

  console.log("smtp "   smtp);

  const mailOptions = {
    from: EMAIL,
    to: email,
    subject: "Facebook email verification",
    html: '<div><div style="max-width:500px;margin-bottom:1rem;align-items:center;display:flex"><img style="width:40px" src="https://seeklogo.com/images/F/facebook-icon-circle-logo-09F32F61FF-seeklogo.com.png" alt=""><span style="font-family:sans-serif;color:#3266a8;font-weight:700;padding-left:10px">Action Required : Activate your facebook account</span></div><div style="border-top:1px solid;border-bottom:1px solid;border-color:#deb887;padding:10px;padding-bottom:20px"><div style="height:35px"><span style="font-weight:700;font-family:sans-serif">Hello ${name}</span></div><div style="height:28px;font-family:sans-serif;padding-bottom:20px"><span>You recently created a profile on facebook. Please confirm your account</span></div><div style=""><a href={url} style="background-color:#3266a8;color:#fff;border-radius:10px;font-weight:700;font-family:sans-serif;text-decoration:none;font-size:15px;text-align:center;padding:9px;margin-bottom:1.5em">Confirm Your Account</a></div></div></div>',
  };
  console.log("mailOptions"   mailOptions);
  smtp.sendMail(mailOptions, (err, res) => {
    if (err) return err;
    return res;
  });
};

I have properly generated the Oauth playground configurations and have the below in my process.env

EMAIL=***
MAILING_ID=***
MAILING_SECRET=***
MAILING_REFRESH=***
MAILING_ACCESS=***

I am getting the below error.

GaxiosError: invalid_grant
***
***

data: {
      error: 'invalid_grant',
      error_description: 'Token has been expired or revoked.'
    },

I am totally beginner with the Google OAuth procedure. Which token has expired? Is it Access token or Refresh Token.

Thanks in advance, Dear Developers community..

CodePudding user response:

The refresh token you are using has expired. You need to request a new one. If your project is still in testing refresh tokens only last for seven days.

You need to set it to production in Google cloud console under the OAuth screen and your refresh tokens will no longer expire

I applaud your use of xOAuth2 with the smtp server

  • Related