Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

341
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda