【问题标题】:Why do only OAuth 2.0 Playground access tokens work for Google API?为什么只有 OAuth 2.0 Playground 访问令牌适用于 Google API?
【发布时间】:2016-07-16 03:10:28
【问题描述】:

我使用以下信息创建了 OAuth 2.0 Playground 访问令牌:

  1. 选择和授权 API:https://www.googleapis.com/auth/consumersurveyshttps://www.googleapis.com/auth/userinfo.email

  2. 获取https://www.googleapis.com/consumersurveys/v2/surveys

  3. 这行得通(对我来说,它会返回我之前创建的调查列表)。

但是,当我使用 Postman 创建访问令牌或从 AspNetUserClaims 表中检索它们时,这些访问令牌不起作用。

示例 #1:我在 Postman for Google 中获得了一个访问令牌,并将其添加到标头(承载和令牌出现复选标记)。我在 Postman 中按 Send,它返回“Invalid_Credentials”。如果令牌已过期或无效,我会删除它并创建一个新的以在标头中使用。仍然失败。

POSTMAN info:
Auth URL: https://accounts.google.com/o/oauth2/auth
Access Token URL: https://accounts.google.com/o/oauth2/token
Client ID: hidden
Client Secret: hidden
Scope: https://www.googleapis.com/auth/userinfo.email
Grant Type: Authorization Code
Request access token locally is checked.

示例 #2:我使用开发站点上的 Google 登录按钮生成访问令牌,然后将其存储在 AspNetuserClaims 表中。我将该访问令牌复制到 Postman(也出现复选标记)并按发送,它也返回“Invalid_Credentials”。如果令牌过期,我会从所有 AspNet 用户表中删除新创建的帐户和访问令牌,然后重试。仍然失败。

为什么这仅适用于 Postman 中的 OAuth 2.0 Playground 令牌?它们都是通过 Postman 令牌向导或新注册的用户帐户或 OAuth2.0 Playground 向导新生成的令牌,但只有 OAuth2.0 Playground 令牌真正起作用...

【问题讨论】:

    标签: asp.net-mvc google-api postman oauth2-playground


    【解决方案1】:

    想通了。

    我认为问题在于 Postman 中的访问令牌需要更多范围来完全验证我的身份,这是有道理的,因为此 API 包含我尝试访问的调查,这些调查也链接到 Google 帐户。这只有在我添加了 consumersurveys.readonly 范围(使用空格)和 userinfo.email 范围后才开始工作,如下所述。

    仅此范围设置不起作用: https://www.googleapis.com/auth/userinfo.email

    对这个 API 有更多权限的这个 SCOPE SETTING 确实有效! https://www.googleapis.com/auth/userinfo.emailhttps://www.googleapis.com/auth/consumersurveys.readonly

    更多关于向 C# 代码添加作用域的信息可以在这里找到:http://www.oauthforaspnet.com/providers/google/

    希望这对遇到类似问题的其他人有所帮助。

    【讨论】:

      猜你喜欢
      • 2016-02-19
      • 1970-01-01
      • 2017-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-29
      • 1970-01-01
      相关资源
      最近更新 更多