get gmail account contacts using oauth 2.0 in java

In yahoo account contacts list with openid and OAuth we saw how to import yahoo account contacts. This may be useful in many scenario like inviting contacts on you website , sending some kind of information to user’s contacts etc.

In this post we will see how to import your google account contacts using Oauth 2.0. There are three basic steps :-

  1. Get user consent code
  2. Use user consent code to obtain access_token
  3. Use access_token to get data from particular API.

To retrieve contacts we need to create application project. To create project login in your google account and visit here.

Now in order to create new project select create new project  as shown below create new project gmail contact import

In next step enter project name and check i agree checkbox shown belowcreate new project gmail account contact import

In a few second your project will be created and you will see it as selected project as shown below.

select project gmail account contact import

Select your project from list and then click on APIs & auth section on LHS as shown below.

APIs and auth gmail account contact import

Click on credential. and select OAuth 2.0 client ID as shown belowAPI credential

We need to configure consent screen for userconfigure consent screen

In next screen fill the detail that will be visible to user. This logo will be visible when user will be prompted to Authorize your App. These all can be changed later. After filling details click on Save.project name

Select Application type as Web Application. and file name in next screen and click Create. Authorized redirect URIs can be changed later.create

Your project is ready now.credential project gmail account contact import

Click on project name to see client id and client secret. You can also change your redirect URI here.credential key and secret project gmail account contact import

Now just one more step in Application creation which is to enable API. Click on APIs in APIs & auth section and then click on contact API as shown below.contact api google enable

In next screen click on Enable API button

enable api

That all we are done with creation of Application. Note down client id and client secret. These are everything for us.

Now let see small coding part. First of all we need to get user consent code.


Simple link to redirect user for consent. When user accept and allow app to use contact then user will be redirected to redirect_uri that we give as an parameter. Note that this redirect url and the uri that we specify in google application that we have created must be same. Otherwise google will give mismatch redirect uri error.


When user give consent to use contact the user will be redirected at this jsp page as we have mention it in redirect uri. We use user consent token to get access token and then use that access token to retrieve actual information.In this case we will retrieve user contacts of google account.

Scope of all google APIs is available here . Main point i have already covered in code comments. Still there are some point that i will revise for more clarity. Above code is available at javafreakers github directory.

Google API Scope
Calendar Data API http(s)://www.google.com/calendar/feeds/
Contacts Data API http(s)://www.google.com/m8/feeds/
Documents List Data API http(s)://docs.google.com/feeds/
Sites Data API http(s)://sites.google.com/feeds/
Spreadsheets Data API http(s)://spreadsheets.google.com/feeds/
Calendar Resources HTTPS Read Only https://apps-apis.google.com/a/feeds/calendar/resource/#readonly
Groups Rosters HTTPS Read Only https://apps-apis.google.com/a/feeds/group/#readonly
Nicknames HTTPS Read Only https://apps-apis.google.com/a/feeds/nickname/#readonly
Users HTTPS Read Only https://apps-apis.google.com/a/feeds/user/#readonly

When i run this and allow app to access my contacts here are some of my contacts

email and pic

That is all in this post. Hope it was useful. If you have any problem or doubt you can contact me. My contact information is available on About Me page of this site. If there is any suggestion to improve please let me. Have fun keep smiling and enjoy a lot every moment.

About the Author: devender kumar

java/j2EE developer

Comments are closed.