Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

342
Views
Get access token using Refresh token through google API using NodeJS

when the callback URL is called I am getting the token from google API and store it in MongoDB.

exports.authorizedGoogle = async (req, res, next) => {
  const oauth2Client = new google.auth.OAuth2(
    process.env.GOOGLE_CLIENT_ID,
    process.env.GOOGLE_CLIENT_SECRET,
    process.env.GOOGLE_CALLBACK_URL
  );
  
    const code = req.query.code;
    const userId = req.query.state;
    const { tokens } = await oauth2Client.getToken(code);
    oauth2Client.setCredentials(tokens);
    var oauth2 = google.oauth2({
      auth: oauth2Client,
      version: "v2",
    });
    const { data } = await oauth2.userinfo.get();
    if (data && tokens && code && userId) {
      const googleUser = await GoogleAccount.create({
        refreshToken: tokens.access_token,
        id_token: tokens.id_token,
        isActive: true,
        user: userId,
        name: data.name,
        email: data.email,
        googleId: data.id,
        imageLink: data.picture,
      });
      res.status(200).json({ status: "success" });
    }
};

now when I am trying to get the access token using refresh token and google API it's throwing an error " token has been expired or revoked".

const currentGoogleAccount = await GoogleAccount.findOne({
      user: userId,
      isActive: true,
    });


    const oauth2Client = new google.auth.OAuth2(
      process.env.GOOGLE_CLIENT_ID,
      process.env.GOOGLE_CLIENT_SECRET,
      process.env.GOOGLE_CALLBACK_URL
    );

     oauth2Client.setCredentials({
      refresh_token: currentGoogleAccount.refreshToken,
    });

    const drive = google.drive({
      version: "v3",
      auth: oauth2Client,
    });

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

in the line where we storing a google account in MongoDB I was referencing access token to refresh the token field.

  const googleUser = await GoogleAccount.create({
        refreshToken:tokens.refresh_token, //tokens.access_token this was the issue so changed access_token to refresh_token 
        id_token: tokens.id_token,
        isActive: true,
        user: userId,
        name: data.name,
        email: data.email,
        googleId: data.id,
        imageLink: data.picture,
      });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!