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.
The scope I am using is:
email profile https://www.googleapis.com/auth/plus.login
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.
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.
I have been following Prisoner's answer right above, and it helped me... until I received the email from Google Developers about how Google+ API will be shutdown on March 7, 2019.
I scrounged around and found this solution to get the email using an
id_token
that is returned when you authorize an app with theemail
scope on your developer console.From Google Sign-in for Websites:
This will return an array that contains the user information, that also contains the email of the user who logged in. Hope this helps anyone else.
You'll want to add the
https://www.googleapis.com/auth/userinfo.email
scope or replacehttps://www.googleapis.com/oauth2/v2/userinfo
with it. If you're using the HTML example they provide, you can list multiple scopes separated by a space.As of 2017: use the
email
scope. See Authorizing API requests.For signing in with Google using OAuth 2.0, there's no need to make a separate request to get user's email.
When Google calls the callback URL, it provides a
code
in the query string that you could use to exchange for access token and ID token. The ID token is a JWT that contains identity information about the user, which includes the email address.See more information here: https://developers.google.com/identity/protocols/OpenIDConnect
This is actually a bit of a challenge as Google does not provide an email by default. You must specifically request it from Google Plus.
There is a full version in this blog post I wrote: https://medium.com/@jackscott/how-to-use-google-auth-api-with-node-js-888304f7e3a0
Update: December 2018
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.The
userinfo
endpoint is de-deprecated and should provide the info assuminghttps://developers.google.com/identity/sign-in/web/devconsole-project
scope andemail
field.Original Answer
There are a lot of issues here in what you're doing and how you're trying to do it.
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.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".