I have had a number of comments on my GMail and Google+ posts recently that ask a simple question or with a simple statement.
How do I find the Authenticated users Email address?
I want to extract user’s email(s) can you help me?
A simple enough question I thought we would take a look at exactly how we return just a users email address from the Google APIs. In order to access the users email address you need authenticate with the scope “email”
EMail This scope requests that your app be given access to:
The user’s Google account email address. You access the email address by calling people.get, which returns the emails array (or by calling people.getOpenIdConnect, which returns the email property in OIDC-compliant format).
the name of the Google Apps domain, if any, that the user belongs to. The domain name is returned as the domain property from people.get (or hd property from getOpenIdConnect).
This email scope is equivalent to and replaces the https://www.googleapis.com/auth/userinfo.email scope.
Also see the related scope: https://www.googleapis.com/auth/plus.profile.emails.read.
So after reading that we know we will need to:
- Request permission to see a users email address
- Use the Google+ API people.get method, to request the email from Google.
It may seam strange to you and in fact its strange to me as well that we wouldn’t use the GMail API to get this information back. Does anyone have any opinions as to why Google chose to place this information in the Google+ API as opposed to the GMail API?
In this post I am going to be using Visual Studio with C# and the Google .net client library. I may create a second post later for my PHP readers out there if they need it.
Create a visual Studio project make sure that its .Net version 4.0 or 4.5 and import the following NuGet package.
PM> Install-Package Google.Apis.Plus.v1
Step One: Authentication
You should already know how to create a client id and client secret in the Google Developer console if you don’t you should check out one of my other tutorials on how to start developing with Google.
As you can see the only scope I sent was Email. Now the strange thing that happens is you would think when we run this the user would be asked to give you permission to see there email.
The is asked to give you offline access. When I saw that I asked myself. “What is offline access?”
In some cases, your application might need to access a Google API when the user is not present. Examples of this include backup services and applications that make Blogger posts exactly at 8 am on Monday morning. This style of access is called
offline, and web server applications can request
offlineaccess from a user. The normal and default style of access is called
Once again Google is confusing us and our users. It doesn’t say anything about having access to their email address.
Step 2: Getting the email
As you can see from the above example I created a Google+ service named plusService. The plusService is authenticated to my user if I want to see that users email address I will need to use the Google+ API method People.Get and pass it me
Top tip: (me is a short cut way of saying give me information on the user I am authenticated with.
var me = plusService.People.Get("me").Execute(); var useremail = me.Emails.FirstOrDefault().Value;
One thing I would like you to note is that me.Emails is a list. A user can have more the one email, if you want the one associated with there Google Account you are after the one with Type of Account
It is possible to get a users email address back using the email scope. We need to use the People.Get method from the Google+ API to request the information once we have an authenticated user using the email scope.
- Why do you think we need to use the Google+ API to retrieve the users email address and not the Gmail API or any other API.
- Do you think it is miss leading to our users that we are requesting email address info and it only displays to them offLine access?
How to get Google email by id ? If I don’t have access token.
By going though the authentication flow you will get an access token.
My name is Ragavan. I’m using Google.Apis.Calendar.V3 and i want to get email address from after log in google calendar.
I got credential service but email address not there. How can i get it please any one help me.
If you do a calendar.get and request “primary” as the calendar id you will be requesting the current authenticated users primary calendar the Id is their email address.