1

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

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

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

  3. 这有效(对我来说,它返回我之前创建的调查列表)。

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

示例 #1:我在 Postman for Google 中获得了一个访问令牌并将其添加到 Header(承载和令牌出现复选标记)。我在 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 令牌有效......

4

1 回答 1

0

想通了。

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

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

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

可以在此处找到有关向 C# 代码添加范围的更多信息:http ://www.oauthforaspnet.com/providers/google/

希望这可以帮助遇到类似问题的其他人。

于 2016-07-16T15:44:24.227 回答