I have a google realtime API web application and I use the "realtime-client-utils.js" library to authenticate that is accessible at GitHub: https://github.com/googledrive/realtime-utils.
When I call RealtimeUtils.authorize(onAuthComplete, usePopup)
I get a response
object for the onAuthComplete
callback method. This response
object contains information about the authorization process:
But I need the email address of the google user that just authenticated himself. Can anybody tell me how I can get the email address?
I tried to use this tutorial: https://developers.google.com/+/web/people/#retrieve-an-authenticated-users-email-address
There are a few ways to retrieve the user's email address. The easiest is to use the id_token returned on the user's access_token that you receive (#3 below).
1) You can use the UserInfo endpoint after including the 'profile' OAuth scope: https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=[token]. Making a dirty query to this API with your access token will return the user's email address.
2) As noted in a different answer, you can use the People.Get API.
3) (In my opinion the best option) Request and use the id_token (JWT) that can be returned along with a valid Google Access Token. For this you must include the 'https://www.googleapis.com/auth/userinfo.email' OAuth scope, and when making the call for Google Authorization, the 'response_type' param must be set to 'token id_token'. You can do this by editing the Realtime API to pass in 'response_type: "token id_token" along with the other params to gapi.auth.authorize.
Once you have your id_token, it needs to be decoded. You can use the code below and just pass in the response object that you receive. The resulting decoded id_token will contain the user's email address.
Good luck!