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,
});