Invite Friends Using Google Contact API and oAuth in PHP

By | October 30, 2015

Invite Friends Using Google Contact API and oAuth in PHP

First, let me introduce what Contacts Data API and OAuth is.

OAuth

OAuth (Open Authorization) is an open standard for authorization. It allows users to share their private resources (e.g. photos, videos, contact lists) stored on one site with another site without having to hand out their credentials, typically username and password.

Google Contact Data APIs

The Contacts Data API allows client applications to view and update a user’s contacts. Contacts are stored in the user’s Google Account; most Google services have access to the contact list.

You can easily find many source codes for importing Gmail contacts by Googling. But most of them use outdated Google Contact Data API or OAuth. By using the latest APIs we can easily import the contacts with fewer lines of code and is much more efficient than the old version. OAuth is in its second generation today. OAuth 2.0 focuses on client developer simplicity while providing specific authorization flows for web applications, desktop applications, mobile phones, and living room devices. OAuth 2.0 is not backward compatible with OAuth 1.0. Using the older APIs you couldn’t retrieve the contacts in local host. That is, it was difficult to modify or edit the code for the website as the code had to be uploaded again and again to the host. But with the help of new API you can try and edit the code in local host and it works efficiently.

invite-friends-using-google-contact-api-and-oauth

How To Login With Google Account oAuth in PHPHow To Login With Google Account oAuth in PHP

In this tutorial we will be using OAuth 2.0 and Google Contacts Data API.

You can refer more about them at the links below.

http://code.google.com/apis/accounts/docs/OAuth2.html

http://code.google.com/apis/contacts/

Step 1:

Create file ‘oauth.php’ in your web-host. For the example that I have shown, I made a folder ‘oauth’ in localhost and created the file ‘oauth.php’ in the folder.

Thus, in my case, the URL for the file is http://localhost/invite-friends-using-google-contact-api-and-oauth/invite-gmail-contacts.php

Please note your URL for the file, as you will need it in the later steps.

Now paste the code below into ‘invite-gmail-contacts.php’ and save the file.

Step 2:

To use the Google API, you need to register your product at Google. To start the Registration process, head to Google API Console.

If you haven’t created any projects yet, then you will be seeing a page as shown below. Click ‘Create Project…’ button.

If you have created a project earlier, then you will be taken to an existing project directly. Click on the drop down menu that has the current project’s name which is located at the top left of the webpage (In my case it is ‘API Project’) and click ‘Create…’ in ‘Other projects’ tab. Now you will be asked to enter the name of the project. Enter a name as you wish.

How To Login With Google Account oAuth in PHP

Now Go to APIs and Then Find Contacts API and Enable it by Clicking Enable.

chainwebber3

Enable Options :

chainwebber4

Step 3:

Now Go to Credentials options and set your Registered Call back URLs and Note Down Client Secret and Client ID:

How To Login With Google Account oAuth in PHP

Step 4:

Open invite-gmail-contacts.php.php that you created in Step 1.

Replace ‘Your Client ID goes here’, ‘Your Client secret goes here’ and ‘Your Redirect URIs goes here’ with the Client ID, Client secret and Redirect URIs that you created in Step 3, respectively.

You can optionally change the number next to ‘$max_results’. It specifies the maximum number of email addresses to be retrieved.

Step 5:

Create the button or link that the user needs to click to retrieve the contact list and set its href parameter to

Replace ‘your_client_id_goes_here’ and ‘

’ with the Client ID and Redirect URIs that you created in Step 7, respectively.

Or you can optionally copy the code given below, replace ‘your_client_id_goes_here’ and ‘your-redirect-url’ as mentioned above, and save it as a new file, may be ‘index.php’.

Then save it to your host.

We have now successfully completed the web application to retrieve Gmail or Google contacts.

Testing the web application:

Now it’s the turn to test the application. Please follow the steps mentioned below.

Step 6:

Browse to the file that created in Step 5 using your favorite web browser. Click on the button or link that you created to retrieve the contacts.

There is a chance that you get an error similar to the one shown below, especially if you are trying it on localhost.

 

If you are among the unlucky ones, please follow the procedure mentioned below to solve the issue.

Solution:

If you are on localhost open:

php/php.ini

php/browscap/php.ini (if existent)

php/php4/php.ini (if existent)

apache/bin/php.ini (if existent)

 

If you are on web host, open web host’s ‘php.ini’

 

Search for ‘extension=php_curl.dll’.

If not found add it to ‘Dynamic Extensions’ section.

If found, uncomment the statement.

Then restart Apache if on localhost.

Try Step 10 again.

Step 7:

You will be taken to a conformation page which will look similar to the one shown below.

chainwebber1

Click ‘Allow Access’ button.

If everything went right, you will have a list of email addresses on screen and a Invite buton also .

Please comment below, if you have any queries or errors.