使用 NodeJS 通过 google API 使用刷新令牌获取访问令牌
Get access token using Refresh token through google API using NodeJS
当调用回调 URL 时,我从 google API 获取令牌并将其存储在 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" });
}
};
现在,当我尝试使用刷新令牌获取访问令牌时,google API 抛出错误“令牌已过期或撤销”。
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,
});
在我们在 MongoDB 中存储 google 帐户的行中,我引用访问令牌来刷新令牌字段。
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,
});
当调用回调 URL 时,我从 google API 获取令牌并将其存储在 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" });
}
};
现在,当我尝试使用刷新令牌获取访问令牌时,google API 抛出错误“令牌已过期或撤销”。
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,
});
在我们在 MongoDB 中存储 google 帐户的行中,我引用访问令牌来刷新令牌字段。
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,
});