为什么只有 OAuth 2.0 Playground 访问令牌适用于 Google API?

Why do only OAuth 2.0 Playground access tokens work for Google API?

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

  1. Select & 授权API:https://www.googleapis.com/auth/consumersurveys https://www.googleapis.com/auth/userinfo.email

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

  3. 这很有效(对我来说 returns 我之前创建的调查列表)。

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

示例 #1:我在 Postman 中为 Google 获取访问令牌并将其添加到 Header(不记名和令牌出现复选标记)。我在 Postman 中按发送,它 returns "Invalid_Credentials"。如果令牌已过期或无效,我将其删除并创建一个新令牌以在 header 中使用。仍然失败。

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 Sign-On 按钮生成一个访问令牌,然后将其存储在 AspNetuserClaims table 中。我将该访问令牌复制到 Postman 中(也出现了一个复选标记)并按发送,它也 returns "Invalid_Credentials"。如果令牌过期,我会从所有 AspNet 用户 table 中删除新创建的帐户和访问令牌,然后重试。仍然失败。

为什么这只适用于 Postman 中的 OAuth 2.0 Playground 令牌?都是通过Postman token wizard或者新注册的用户账号或者OAuth2.0 Playground wizard新生成的token,但是只有OAuth2.0 Playground tokens才真正起作用...

想通了。

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

仅此 SCOPE SETTING 无效: https://www.googleapis.com/auth/userinfo.email

具有更多权限的范围设置 API 成功了! https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/consumersurveys.readonly

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

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