web-services - 如何验证 Google 身份验证 API 访问 token ?

标签 web-services oauth google-oauth google-authentication

如何验证 Google 身份验证访问 token ?

我需要以某种方式查询 Google 并询问:[给定的访问 token ] 对于 [example@example.com] Google 帐户是否有效?

简短版本

很清楚如何通过 Google Authentication Api :: OAuth Authentication for Web Applications 提供访问 token 然后可用于从一系列 Google 服务请求数据。目前尚不清楚如何检查给定的访问 token 对于给定的 Google 帐户是否有效。我想知道怎么办。

长版

我正在开发一个使用基于 token 的身份验证的 API。提供有效的用户名+密码或从任何一项N可验证服务提供第三方 token 后,将返回 token 。

第三方服务之一是 Google,它允许用户使用他们的 Google 帐户针对我的服务进行身份验证。稍后将扩展到包括 Yahoo 帐户、受信任的 OpenID 提供商等。

基于 Google 的访问的示意图示例:

alt text

“API”实体处于我的完全控制之下。 “公共(public)界面”实体是任何基于网络或桌面的应用程序。一些公共(public)接口(interface)在我的控制之下,其他的则不会,还有一些我可能永远不知道。

因此,我无法信任第 3 步中提供给 API 的 token 。该 token 将与相应的 Google 帐户电子邮件地址一起提供。

我需要以某种方式查询 Google 并询问:此访问 token 对于 example@example.com 是否有效

在本例中,example@example.com 是 Google 帐户唯一标识符 - 用户用于登录其 Google 帐户的电子邮件地址。不能将其假定为 Gmail 地址 - 某人可以在没有 Gmail 帐户的情况下拥有 Google 帐户。

Google 文档清楚地说明了如何使用访问 token 从多种 Google 服务检索数据。似乎没有任何内容说明如何首先检查给定的访问 token 是否有效。

更新 该 token 对 N 个 Google 服务有效。我无法尝试针对 Google 服务使用 token 来验证它,因为我不知道给定用户实际使用的是所有 Google 服务的哪个子集。

此外,我永远不会使用 Google 身份验证访问 token 来访问任何 Google 服务,而只是将其作为验证假定的 Google 用户是否真实身份的一种手段。如果有其他方法可以做到这一点,我很乐意尝试。

最佳答案

对于用户检查,只需发布 获取访问 token 作为 accessToken 并将其发布并获取响应

https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=accessToken

你也可以在浏览器的地址栏中尝试,也可以在java中使用httppost和response

响应将类似于

{
     "issued_to": "xxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
     "audience": "xxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
     "user_id": "xxxxxxxxxxxxxxxxxxxxxxx",
     "scope": "https://www.googleapis.com/auth/userinfo.profile https://gdata.youtube.com",
     "expires_in": 3340,
     "access_type": "offline"
    }

范围是accessToken的给定权限。您可以在this link中检查范围ID

更新: 新的API 帖子如下

https://oauth2.googleapis.com/tokeninfo?id_token=XYZ123

响应将如下

 {
 // These six fields are included in all Google ID Tokens.
 "iss": "https://accounts.google.com",
 "sub": "110169484474386276334",
 "azp": "1008719970978-hb24n2dstb40o45d4feuo2ukqmcc6381.apps.googleusercontent.com",
 "aud": "1008719970978-hb24n2dstb40o45d4feuo2ukqmcc6381.apps.googleusercontent.com",
 "iat": "1433978353",
 "exp": "1433981953",

 // These seven fields are only included when the user has granted the "profile" and
 // "email" OAuth scopes to the application.
 "email": "testuser@gmail.com",
 "email_verified": "true",
 "name" : "Test User",
 "picture": "https://lh4.googleusercontent.com/-kYgzyAWpZzJ/ABCDEFGHI/AAAJKLMNOP/tIXL9Ir44LE/s99-c/photo.jpg",
 "given_name": "Test",
 "family_name": "User",
 "locale": "en"
}

欲了解更多信息,https://developers.google.com/identity/sign-in/android/backend-auth

关于web-services - 如何验证 Google 身份验证 API 访问 token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/359472/

相关文章:

android - Google Developer Console - 同意屏幕 - 自定义产品 Logo 不起作用

java - 外部应用程序使用 Java Web 服务进行 Moodle 身份验证

php - 将 arraylist bean 从 android 传递到 webservice php

android - android 中 oauth 后的 Twitter 用户屏幕名称

google-api - 如何在使用 Google API JS 客户端时获取刷新 token

python - 使用 OAuth2 在 gspread 中使用 python 进行授权的问题

apache-flex - 灵活的网络服务客户端

java - 是否需要为客户端和服务器代码指定 MTOM

android - Instagram API OAuth 和端点更新 04-30-2015

java - 如何使用 OAuth2 向 Mendeley 进行身份验证