node.js - 如何将 Passport.js Google OAuth2 策略与 Google api 一起使用?
问题描述
我已将护照 google oauth 2.0 添加到我的网站,它工作正常。
这是对 google 的初始身份验证调用:
router.get("/google",
passport.authenticate('google',
{ scope: ["https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/auth/userinfo.profile"]
}));
但是当我访问 Drive API 时,我得到一个错误。
例如,当我使用:
const drive = google.drive({ version: "v3", auth});
drive.files.create(
{
.......
}
我收到一个错误:
code: 401, errors: [ { domain: 'global', reason: 'required', message: 'Login Required', locationType: 'header', location: 'Authorization' } ]
有没有办法在登录期间使用令牌并协调护照登录和 api 访问?
解决方案
你auth
的例子中有什么?
你有没有尝试过:
const oauth2Client = new google.auth.OAuth2()
oauth2Client.setCredentials({
'access_token': req.user.accessToken
});
const drive = google.drive({
version: 'v3',
auth: oauth2Client
});
推荐阅读
- c - 计算字符串中所有数字的总和
- r - 堆叠条中的 X 轴和右框
- node.js - NodeJS 集群和工作线程
- android - 如何从布局(视图)创建 GIF 文件?
- css - Centos7、Apache、Mod Passenger 和 Ruby 2.6.3 的权限被拒绝
- jenkins - 如何在詹金斯中止之前做一个动作
- r - 从列表列表中创建数据框,但每个元素都是一列
- javascript - 如何在 JavScript 中访问 Rails 模型属性?
- python - 为 MultiIndex 中的每个唯一元素重复 DataFrame 的索引
- android-databinding - androidx.navigation:navigation-fragment:2.2.0, get "You must call removeView() on the child's parent first." 在 navController.popBackStack()