首页 > 解决方案 > 如何从护照回调中提取其他 API 范围?

问题描述

我想通过passport-google-oauth20策略获取使用 Passport.js 的用户的生日。我从哪里得到这个额外范围的响应以及如何提取这些信息?

我在从用户那里提取个人资料和电子邮件方面取得了成功,但我尝试的所有其他范围似乎都找不到它返回的位置。我正在使用Google Identity Platform中的一些范围(更具体地说,是 People API,我已经从 Google Cloud Platform 激活了它)并且它们没有出现在 Passport 策略回调中(仅个人资料和电子邮件)。

这是我设置护照并检查返回的内容

passport.use(
  new GoogleStrategy(config, (accessToken, refreshToken, profile, done) => {
    console.log(profile);
    // outputs:
    // id: ...
    // displayName: ...
    // name: { ... }
    // emails: [ { ... } ]
    // photos: [ { ... } ]
    // _raw: ...
    // _json: ... (above info plus profile link and locale)

    ...
  })
);

在我的路线中,我声明了范围并重定向用户

router.get('/auth/google/', passport.authenticate('google', {
    scope: ['https://www.googleapis.com/auth/user.birthday.read', 'profile', 'email']
}));

router.get('/auth/google/redirect/', passport.authenticate('google'), (req, res) => {
  res.redirect('http://localhost:2000/profile/' + req.user.id)
})

我的 node 和 npm 版本目前是:

node --version
v8.12.0

npm --version
6.9.0

我的包版本如下:

...
"express": "^4.16.4",
"passport": "^0.4.0",
"passport-google-oauth20": "^2.0.0",
...

据我了解,我应该能够使用 Passport 检索个人资料、电子邮件和 openid 以外的更多信息,并且我知道我需要获取我想要激活的范围的 API。我不明白的是如何在回调函数中从该 API 检索信息或如何检索该信息。

标签: javascriptnode.jsexpresspassport.jspassport-google-oauth2

解决方案


推荐阅读