本文介绍了Google OAuth API 获取用户的电子邮件地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用自己的个人 Google 帐户玩 Google 的 OAuth 2.0 Playground,但我似乎无法使用 Playground 恢复我的 Gmail 地址.

I am playing with Google's OAuth 2.0 Playground using my own personal Google account, but I cannot seem to recover my Gmail address using the playground.

我使用的范围是:

email profile https://www.googleapis.com/auth/plus.login

但是当我调用 API 时:

But when I call the API:

https://www.googleapis.com/oauth2/v2/userinfo

我获得了有关用户的各种信息,例如姓氏、名字、性别、图片等,但它不会返回用户的电子邮件.

I get various information about the user such as family name, first name, gender, picture, etc. but it does not return the user's email.

如何检索用户的电子邮件地址?是我的范围错误还是调用了错误的 API?我觉得这应该很简单,但我实际上已经尝试了几个小时来解决这个问题,但我找不到始终提供用户电子邮件地址的 API 和范围组合.

How do I retrieve the user's email address? Do I have the wrong scope or am I calling the wrong API? I feel like this should be very simple but I have literally been trying to figure this out for hours and I cannot find an API and scope combination that consistently provides the user's email address.

推荐答案

更新:2018 年 12 月

12 月 20 日,Google 宣布 Google+ API 将于 2019 年 3 月被拒绝,从 2019 年 1 月底开始出现间歇性故障.作为 plus.people.get 的一部分code> 端点已弃用并计划终止.

On December 20th, Google announced that the Google+ API would be turned down in March 2019, with intermittent failure starting at the end of January 2019. As part of the the plus.people.get endpoint is deprecated and scheduled to be terminated.

userinfo 端点已弃用(请参阅说明),并应提供信息假设

The userinfo endpoint is de-deprecated (see clarification) and should provide the info assuming

  1. 您请求 https://developers.google.com/identity/sign-in/web/devconsole-project 范围和
  2. 您请求 email 字段.
  1. You request the https://developers.google.com/identity/sign-in/web/devconsole-project scope and
  2. You request the email field.

说明:2019 年 1 月 24 日

谷歌 记录 userinfo (v2) 端点已被弃用,但后来将其更改为已弃用,但为了向后兼容而保持可用".

Google documented that the userinfo (v2) endpoint was deprecated, but later changed it to "deprecated, but kept available for backwards compatibility".

当前文档 讨论了通过当前支持的openid 方法.这包括使用在其发现文档中指定的userinfo"端点,如OpenID 连接.

Current documentation discusses getting profile and email information through the currently supported openid method. This includes using the "userinfo" endpoint specified in their discovery document, as required by OpenID Connect.

目前,该 URL 为 https://openidconnect.googleapis.com/v1/userinfo,但这在过去和 https://account.google.com/.well-known/openid-configuration 是要使用的 URL 的权威来源.

At the moment, that URL is https://openidconnect.googleapis.com/v1/userinfo, but this has changed in the past and the discovery document at https://accounts.google.com/.well-known/openid-configuration is the authoritative source for the URL to use.

所以,要清楚:

  • 保留旧的 userinfo URL 是为了向后兼容
  • 发现文档中提供了新的 userinfo URL

无论如何,任何内容的 plus 版本(如下所述)已弃用并计划将其删除.

Regardless, the plus version of anything (described below) is deprecated and scheduled to be removed.

原答案

这里有很多关于你正在做什么以及你如何去做的问题.

There are a lot of issues here in what you're doing and how you're trying to do it.

对于初学者来说,https://www.googleapis.com/oauth2/v2/userinfo 端点已被弃用,并计划于 2014 年 9 月删除.它开始不一致地工作 - 所以不要不要使用它.

For starters, the https://www.googleapis.com/oauth2/v2/userinfo endpoint is deprecated, and scheduled to be removed in September 2014. It has begun working inconsistently - so don't use it.

正如@abraham 所指出的,您将使用 people.get https://www.googleapis.com/plus/v1/people/me 上的端点.这应该为您提供包含地址数组的 emails 字段.在您的情况下,可能只有一个具有帐户"类型.

As @abraham noted, you'll use the people.get endpoint at https://www.googleapis.com/plus/v1/people/me. This should give you the emails field containing an array of addresses. In your case, there will likely be only one that has a type of "account".

这篇关于Google OAuth API 获取用户的电子邮件地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-01 11:30