cuando se llama a la URL de devolución de llamada, obtengo el token de la API de Google y lo almaceno en 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" }); } };ahora, cuando intento obtener el token de acceso usando el token de actualización y la API de Google, arroja un error "el token ha caducado o revocado".
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, });en la línea donde almacenamos una cuenta de Google en MongoDB, hacía referencia al token de acceso para actualizar el campo del token.
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, });