首页 > 解决方案 > Passport.js 对 Gmail API 进行身份验证导致 GooglePlusAPIError

问题描述

我使用 Passport.js 向 Google 进行身份验证。我定义了以下范围:

passport.authenticate('google', {
  scope: [
    'https://www.googleapis.com/auth/gmail.modify',
  ],
})

所以,我只想访问 Gmail API。

现在,谷歌回应:

GooglePlusAPIError: Insufficient Permission

当我通过 Google API 仪表板将 Google+ API 添加到我的项目中并将以下范围添加到我的 passport.authenticate 调用时,我得到了它:

'https://www.googleapis.com/auth/userinfo.email'

但是,当我只想访问 Gmail 时,为什么还需要 Google+ API?

我还找到了这个网站:https ://developers.google.com/gmail/api/auth/web-server他们在其中设置了这些范围:

SCOPES = [
  'https://www.googleapis.com/auth/gmail.readonly',
  'https://www.googleapis.com/auth/userinfo.email',
  'https://www.googleapis.com/auth/userinfo.profile',
  # Add other requested scopes.
]

我错过了什么?

提前致谢!

干杯,尼尔斯

编辑:

我刚刚研究了我的 Google API 仪表板并找到了下面的屏幕。那么,这些范围是强制性的吗?

我的 Google API 仪表板的屏幕截图

编辑 2

上下文是一个 Express.js 应用程序。整个调用看起来像:

router.get('/google', passport.authenticate('google', {
  scope: [
    'https://www.googleapis.com/auth/gmail.modify',
  ],
}));

router.get('/google/callback', passport.authenticate('google', {
  failureRedirect: '/',
}), (req, res) => {
  req.session.token = req.user.token;
  res.redirect('/');
});

标签: oauth-2.0google-apipassport.jsgoogle-plusgmail-api

解决方案


推荐阅读