Import Google contacts with PHP or Javascript using Google Contacts API and OAUTH 2.0

Inviting friends to a web application is a key factor to gain more users and traffic.

In this article we will try to give a practical, step by step web development example on how you can easily import google contacts to your web application, because even if google offers an api documentation, it’s always hard to find an example that is not out-dated,  that fits in your app without having headaches and losing precious time.

This tutorial will offer examples for Server Side application (php) but also for Client Side application (javascript). We will use OAUTH 2.0 authentication and Google Contacts API version 3.0.

Import Google contacts with PHP or Javascript using Google Contacts API and OAUTH 2.0

View on githubYou can also view the source code on Github

View demoView live example


Step 1

Create a Google application in Google Developers Console for obtaining your Client id and Client secret.

  • Go to Google Google Developers Console and login with your google account.
  • If you don’t have already a project, create one

Google Console, create project

  • Go into the newly created project
  • In the left menu, go to APIs & auth > API’s

Google Console APIs

  • Search for “Contacts API” and click on it
  • Enable the “Contacts API” by hitting the “Enable API” button

Enable Contacts API Google developers

  • Go to “Consent screen” if you didn’t set a product name already. In the left menu, go to APIs & auth > Consent screen
  • Configure your “Consent screen” settings

Configure Consent screen

  • Create a new OAuth Client ID. In the left menu, go to APIs & auth > Credentials
  • Click on “Create a new Client ID”

Create Client ID Google API

  • For “Application type” select “Web Application” and enter a proper URL for “Authorized javascript origins” and “Authorized Redirect URIs” and click “Create Client ID”.
    • Note: It’s important that you enter your correct url application or “Authorized Javascript origins”, something like http://www.example.com.
    • The url that you will enter on “Authorized redirect URIs” , will be the url that google will use to redirect with your response data. For example, after a user will accept giving credentials to your application, will be automatically redirected to http://www.example.com/response-callback.php . In this file you can read the google contacts.
  • If everything goes well, you will now able to see your API credentials, Client ID and Client Secret

Google API Client ID, Client secret

 

Step 2

Server side example using PHP

  • Use a real, online server to integrate this example. Localhost server will not work.
  • download and include “Google APIs Client Library for PHP” in your php application. We will use a file name “response-callback.php” located in the root application, for the example bellow.

  • Declare your Google Client ID, Google Client secret and Google redirect uri in  php variables
  • It’s important that the redirect uri to be added be added in Google Developers Console , in your Authorized redirect URIs

  • setup new google client

  • create a curl function

  • Google will redirect back to our URL with a code token parameter, for example: http://www.example.com/response-callback.php?code=1241254
  • For better UX, we can store the received code into a session, and redirect back to this url

  • Check if we have session with our token code and retrieve all contacts, by sending an authorized GET request to the following URL : https://www.google.com/m8/feeds/contacts/default/full
  • Upon success, the server responds with a HTTP 200 OK status code and the requested contacts feed. For more informations about parameters check Google API contacts documentation

  • create the link that the user will click to import his contacts

  • when the user will click on that link he will be asked to allow permissions for your application

Google API request permission

  • the user will be automatically redirected to your application, and you can display his contact list

 

UPDATE: How to import contact photos using Google Contact API

Google documentation about retrieving a contact’s photo.

To retrieve a contact’s photo, send an authorized GET request to the contact’s photo link URL.
Considering that we already have the contact photo link in our $contacts array above, we just need to import the photo with a GET request using PHP.

When parsing the contacts in the php foreach, we will retrieve also the image for each contact. The foreach code will become:

 

Step 2 (alternative) :

Client side example using Javascript

According to Google documentation about “OAuth 2.0 for Client-side Applications” , your application should always use HTTPS when using client side implementation.

  • authorize access and fetch contacts

  •  Make a button to fire the event

 

Still having problems importing your contacts using Google API?

Don’t hesitate to post your question bellow, in the Comments section.

 

New, on our blog: A tutorial on importing Gmail contacts with PHP or Javascript using Google Contacts API and OAUTH 2.0…

Posted by Design19 on Monday, May 4, 2015

 

 

Bogdan Rusu

Working in development for more then 10 years, Bogdan Rusu is the CTO of Design19 web agency. Highly skilled in PHP development and usage of CMS like Wordpress, Magento, Zend framework, but also custom built platforms based on PHP, Bogdan has driven the team to a higher level of proficiency in development.