【问题标题】:Enable Google API for "googleapis.com/auth/userinfo"为“googleapis.com/auth/userinfo”启用 Google API
【发布时间】:2018-07-15 22:01:51
【问题描述】:

我正在开发 Google Chrome 扩展程序。我希望能够使用来自这些端点的数据访问用户个人资料信息:

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

https://www.googleapis.com/oauth2/v1/userinfo

我似乎无权访问这些资源。从我的网络搜索来看,我可能需要在 Google Developers Console 中启用库 API。但我看了那里,找不到合适的 API 来启用。有谁知道我应该启用哪个 API?

【问题讨论】:

  • 在使用https://www.googleapis.com/oauth2/v1/userinfo的情况下,不需要开启与此相关的API。您可以通过从 OAuth2 进程中检索到的访问令牌来使用端点。当您使用它时,请将https://www.googleapis.com/auth/userinfo.profile 包含在范围内。如果此信息对您没有用处,我很抱歉。
  • 这是附加信息。作为测试,我在未启用所有 API 的情况下使用它。示例 curl 命令是 curl -H "Authorization: Bearer ### access token ###" "https://www.googleapis.com/oauth2/v1/userinfo"。我确认它有效。令牌的范围只有https://www.googleapis.com/auth/userinfo.profile。关于端点,https://www.googleapis.com/oauth2/v1/userinfo 和 https://www.googleapis.com/oauth2/v2/userinfo 返回相同的信息。
  • 你知道OP中两个url有什么区别吗?
  • 关于userinfo,返回相同的信息。 v3也是一样的。似乎除了userinfo之外的端点,版本之间存在差异。 stackoverflow.com/questions/31277898/…

标签: google-chrome-extension google-api google-oauth google-developers-console


【解决方案1】:

您至少需要按照所述添加范围 https://www.googleapis.com/auth/userinfo.profile。

那么你就可以像使用sheets、calendar等一样使用oauth2 api了。

在 nodejs 中,它会是这样的:

    const { google } = require('googleapis')
    const auth = new google.auth.OAuth2(
      keys.web.client_id,
      keys.web.client_secret,
      keys.web.redirect_uris[0]
    )
    // get the tokens from google
    auth.credentials = tokens
    const oauth2 = google.oauth2({ version: 'v2', auth })
    const userinfo = await oauth2.userinfo.get()
    console.log(userinfo)

API 记录在这里:https://googleapis.dev/nodejs/googleapis/latest/oauth2/interfaces/Schema$Userinfo.html#info。那里的源代码中也有一些示例。

【讨论】:

    【解决方案2】:

    如果您有来自用户的访问令牌,您可以从这些端点检索数据。首先,您必须通过让用户使用 google login API 登录来获取访问令牌。然后从以下链接检索数据。 https://www.googleapis.com/oauth2/v1/userinfo?access_token=PUT_YOUR_USER'S_ACCESS_TOKEN_HERE

    【讨论】:

      【解决方案3】:

      如果您只需要用户的电子邮件地址进行身份验证等,则必须启用 Google 的 Plus API,不是提到的 G**-*****d People API Google Cloud API 文档中的任何地方。

      另外,您需要提及https://www.googleapis.com/auth/userinfo.email 的范围,不是 https://www.googleapis.com/auth/userinfo.profile 之一。

      附言这是一个古老的问题,但没有答案。可能你已经解决了。我是为未来的人发布的,或者只是为了我未来的自己。

      【讨论】:

      • 您应该知道 Google+ 相关的 API 将于 19 年 1 月关闭。更多信息请访问Google blog。
      • 如果 google+ 被关闭...如果我们使用 gmail api 会不安全吗?
      猜你喜欢
      • 1970-01-01
      • 2018-07-05
      • 1970-01-01
      • 2019-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-16
      • 2015-09-07
      相关资源
      最近更新 更多