Find Users email with Google API 4


emailuser

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?

or

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:

  1. Request permission to see a users email address
  2. 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?

Setup

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.

Google Oauth2 Email scope The is asked to give you offline access.   When I saw that I asked myself.   “What is offline access?”

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 offline access from a user. The normal and default style of access is called online.

 

 

 

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

List of users email addresses

Conclusion

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.

Questions

  1. 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.
  2. 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?

About Linda Lawton

My name is Linda Lawton I have more than 20 years experience working as an application developer and a database expert. I have also been working with Google APIs since 2012 and I have been contributing to the Google .Net client library since 2013. In 2013 I became a a Google Developer Experts for Google Analytics.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

4 thoughts on “Find Users email with Google API