尝试通过googleapis和node.js获取经过身份验证的用户电子邮件时出错

问题描述

我正在使用googleapis在我的网站上实现身份验证。函数plus.people.get不起作用。我已经看到它在某些论坛上已被弃用,但仍在google上记录下来,这让我感到困惑。我收到的错误是“以前在项目328985958128中未使用过Legacy People API或已将其禁用。请通过访问https://console.developers.google.com/apis/api/legacypeople.googleapis.com启用它,然后重试。如果最近启用了此API,请等待几分钟以使操作传播进入我们的系统并重试。”该网页甚至没有加载。我的代码

   if (!req.body.token) return res.status(500).json({ type: 'error',message: 'No access token provided.' })
   const OAuth2 = google.auth.OAuth2
   const oauth2client = new google.auth.OAuth2(keys.client_id,keys.client_secret)
   google.options({ auth: oauth2client });
   const plus = google.plus('v1')
   oauth2client.setCredentials({
       access_token: req.body.token
   })
   plus.people.get({
       userId: 'me',auth: oauth2client
   },(error,response) => {
       if (error) 
       console.log(error)
       return res.status(500).json({ type: 'error',error })
       const emails = (response.data || {}).emails

解决方法

您正在使用deprecatedgoogle.plus('v1')

  • 您应该使用

const service = google.people({version: 'v1',auth: oauth2Client})

创建服务对象。

  • 要执行请求,不再需要额外的授权,所以:
service.people.get({
       userId: 'me'
   },(error,response) => {
      ...
  })

更多信息: