简体   繁体   English

Google OAuth2.0 - Gaxios invalid_grant 错误

[英]Google OAuth2.0 - Gaxios invalid_grant error

The below is my mailer.js code下面是我的 mailer.js 代码

    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我已经正确生成了 Oauth 游乐场配置,并在我的 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.我完全是 Google OAuth 程序的初学者。 Which token has expired?哪个令牌已过期? Is it Access token or Refresh Token.是访问令牌还是刷新令牌。

Thanks in advance, Dear Developers community..在此先感谢亲爱的开发者社区..

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.如果您的项目仍在测试刷新令牌仅持续 7 天。

You need to set it to production in Google cloud console under the OAuth screen and your refresh tokens will no longer expire您需要在 OAuth 屏幕下的 Google 云控制台中将其设置为生产,您的刷新令牌将不再过期

I applaud your use of xOAuth2 with the smtp server 👏我赞赏您将 xOAuth2 与 smtp 服务器一起使用👏

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM