如何解决OAuth2Client.getToken始终返回“未定义”
我正在遵循https://github.com/googleapis/google-api-nodejs-client#installation官方指南,使用OAuth2部分中的“身份验证和授权”入门
遵循这些非常简单的几行代码根本无法工作。我已正确重定向,然后登录并允许请求的范围。至此,我被重定向到回调URL,成功解析了查询字符串中的代码,并通过调用oauth2Client.getToken(code)
将其传递给我的OAuth2Client对象。这是我完全迷失的地方,因为无论此方法是什么,总是返回“ undefined”。完全没有错误,只是“未定义”。至少可以说不是很有帮助。
const {google} = require('googleapis');
const oauth2Client = new google.auth.OAuth2(
'hidden','hidden','hidden'
);
/* GET home page. */
router.get('/home',function(req,res,next) {
try {
if (req.url.indexOf('/home') > -1) {
var code = req.query.code;
var error = req.query.error;
var session = req.session;
if(code!=null){
const {tokens} = oauth2Client.getToken(code); //Always returns undefined?
oauth2Client.credentials = tokens;
}
else{
res.redirect('/');
}
}
} catch (e) {
res.redirect('/err')
}
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。