Google OAuth2 无法获取个人资料信息
Google OAuth2 cannot get profile info
我正在使用 https://accounts.google.com/o/oauth2/auth?
端点来获取 id_token
。
范围是openid profile email
。
问题是,当我尝试验证 id_token
时,我得到 iss, azp, aud, sub, email, email_verified, iat, exp, jti
。正如您所看到的,没有任何像 given_name, family_name, picture
.
这样的个人资料信息
official doc 说它应该包含配置文件信息:
// 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"
已授予所有权限。
更新
以防万一有人需要它。
如 Vladimir Serykh 所述,要获取个人资料信息,我们需要点击 /userinfo
端点。但是此端点将 access_token
作为 Authorization
header 中的 Bearer 令牌,因此您也需要获取它。
所以基本上我们需要使用 response_type='id_token token'
查询参数调用 OAuth2。之后 responseUrl 也将包含 access_token
。
接下来您只需调用 https://openidconnect.googleapis.com/v1/userinfo
端点并将授权 header 设置为 Bearer your_access_token
。您将收到包含个人资料信息的回复:
"sub": "user_id",
"name": "Name Lastname",
"given_name": "Name",
"family_name": "Lastname",
"picture": "pic_url",
"email": "example@gmail.com",
"email_verified": true,
"locale": "en"
再次感谢,Vladimir Serykh
不太清楚文档的历史(根据您提供的link)以及它是否与您的案例相关。
我知道不同的身份提供者的工作方式可能略有不同。我知道您应该使用获得的 ID 令牌对 /userinfo
端点进行单独调用以获取用户信息的情况。
Google Identity Platform.
有一些不同的 Google 文档
它描述了 ID 令牌。
https://developers.google.com/identity/protocols/OpenIDConnect#obtainuserinfo
Google ID Tokens may contain the following fields (known as claims):
请注意,Provided 列中没有 always
。我认为对于不同的 API 可能会有所不同。
相同的文档有部分“获取用户配置文件信息”
它解释了从哪里获得 /userinfo
端点 URL 以及如何调用它。在回复中,您应该会收到所需的信息。
我猜它在您的情况下不起作用的原因是您使用的是 /tokeninfo
端点。它不是 OpenID Connect 标准的一部分。它只是验证令牌并解析它(与 https://jwt.io 做同样的工作)。由于某种原因,原始 ID 令牌不包含该声明。因此 /tokeninfo
端点不会 return 给你。
但是根据 Google 的文档,您应该使用 /userinfo
端点来获取用户信息声明。
您可以在 OpenID Connect 规范中找到此端点的描述:
https://openid.net/specs/openid-connect-core-1_0.html#UserInfo
5.3 UserInfo endpoint
The UserInfo Endpoint is an OAuth 2.0 Protected Resource that returns Claims about the authenticated End-User. To obtain the requested Claims about the End-User, the Client makes a request to the UserInfo Endpoint using an Access Token obtained through OpenID Connect Authentication.
我正在使用 https://accounts.google.com/o/oauth2/auth?
端点来获取 id_token
。
范围是openid profile email
。
问题是,当我尝试验证 id_token
时,我得到 iss, azp, aud, sub, email, email_verified, iat, exp, jti
。正如您所看到的,没有任何像 given_name, family_name, picture
.
official doc 说它应该包含配置文件信息:
// 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"
已授予所有权限。
更新
以防万一有人需要它。
如 Vladimir Serykh 所述,要获取个人资料信息,我们需要点击 /userinfo
端点。但是此端点将 access_token
作为 Authorization
header 中的 Bearer 令牌,因此您也需要获取它。
所以基本上我们需要使用 response_type='id_token token'
查询参数调用 OAuth2。之后 responseUrl 也将包含 access_token
。
接下来您只需调用 https://openidconnect.googleapis.com/v1/userinfo
端点并将授权 header 设置为 Bearer your_access_token
。您将收到包含个人资料信息的回复:
"sub": "user_id",
"name": "Name Lastname",
"given_name": "Name",
"family_name": "Lastname",
"picture": "pic_url",
"email": "example@gmail.com",
"email_verified": true,
"locale": "en"
再次感谢,Vladimir Serykh
不太清楚文档的历史(根据您提供的link)以及它是否与您的案例相关。
我知道不同的身份提供者的工作方式可能略有不同。我知道您应该使用获得的 ID 令牌对 /userinfo
端点进行单独调用以获取用户信息的情况。
Google Identity Platform.
有一些不同的 Google 文档它描述了 ID 令牌。
https://developers.google.com/identity/protocols/OpenIDConnect#obtainuserinfo
Google ID Tokens may contain the following fields (known as claims):
请注意,Provided 列中没有
always
。我认为对于不同的 API 可能会有所不同。相同的文档有部分“获取用户配置文件信息”
它解释了从哪里获得
/userinfo
端点 URL 以及如何调用它。在回复中,您应该会收到所需的信息。
我猜它在您的情况下不起作用的原因是您使用的是 /tokeninfo
端点。它不是 OpenID Connect 标准的一部分。它只是验证令牌并解析它(与 https://jwt.io 做同样的工作)。由于某种原因,原始 ID 令牌不包含该声明。因此 /tokeninfo
端点不会 return 给你。
但是根据 Google 的文档,您应该使用 /userinfo
端点来获取用户信息声明。
您可以在 OpenID Connect 规范中找到此端点的描述: https://openid.net/specs/openid-connect-core-1_0.html#UserInfo
5.3 UserInfo endpoint
The UserInfo Endpoint is an OAuth 2.0 Protected Resource that returns Claims about the authenticated End-User. To obtain the requested Claims about the End-User, the Client makes a request to the UserInfo Endpoint using an Access Token obtained through OpenID Connect Authentication.