如何在不使用google
我已经实现了代码来获取access_token,refresh_token和expire_date。所有这些值都已存储在数据库中。
现在我需要一种方法来获取另一个access_token以防旧旧的过期而不使用google-api-nodejs-client包
原因是这个模块最近有bug,你可以在这里查看(我的评论是最新的) 。
此刻,我尝试过这样的事情
GoogleTokens.findOne({isObsolete: false}).then((token) => {
let subscriptionsGetUrl = "/" + req.query.packageName + "/purchases/subscriptions/" + req.query.subscriptionId + "/tokens/" + req.query.token + "?access_token=" + token.accessToken;
request(subscriptionsGetUrl, function (error, response, body) {
if (error) {
throw error;
}
res.success(JSON.parse(body).expiryTimeMillis);
});
});
但是当access_token过期时它会失败。
谢谢。
回答如下:这个答案怎么样?您可以使用OAuth2 API检索访问令牌的到期时间。如果要在没有google-api-nodejs-client的情况下检索它,可以使用以下方法。
示例脚本:
var uri = "https://www.googleapis/oauth2/v3/tokeninfo?access_token=" + accesstoken;
request.get({uri: uri}, (err, res, body) => {
console.log(body)
});
回应:
If the access token can be used yet, the following response is returned.
{
"azp": "#####",
"aud": "#####",
"sub": "#####",
"scope": "#####",
"exp": "1234567890", // Expiration time
"expires_in": "3600", // Remaining time
"access_type": "offline"
}
If the access token has already not been able to used, the following response is returned.
{
"error_description": "Invalid Value"
}
参考:
我从以下文件中得到了上述方法。
- Validating an ID token
- Calling the tokeninfo endpoint
- APIs Explorer
如果这对你没用,我很抱歉。