Read Gmail Contacts Using PHP

Baba Gyani Triviani said:
"It is really good when you make things secure. But it is better when you make things secure without a hassle"

You may have seen various websites allowing you to send invitations to your friends. But surely you don't remember the email addresses of all your friends. And so, they give you a provision to connect that site with Gmail, Yahoo, Facebook etc. Once you click on the link, you will be redirected to an intermediate page of the service (Gmail, Yahoo, FB etc.) where they tell you that the website in question is attempting to retrieve your contacts or other details and if it is ok for the service to share these details (basically, if you trust the site or not).

If you click on Accept and say "Yeah, I know them, they're cool" then you are normally provided with a screen which shows all the email addresses with checkboxes against them so that you can check and send invitations to them.

The following is a quick way for you to do it. What we attempt to do here is - Have a page which has a link that says "Login using Google" (or you can have a big Gmail logo or whatever it is that you like) and another page which will print the email addresses on screen. Instead of printing, you can do fancy things like putting checkboxes next to them and process them subsequently. So, here we go

Note: We attempt to do this using PHP. Hope you have a running webserver.

STEP 1: Go the Google APIs Console

Start using the Google APIs console

  • with a big blue button in the centre that reads Create Project. Go ahead and click it
  • In the panel on the left side, click on API Access
  • You should see a button that reads "Create an OAuth 2.0 client ID..." Click that.
  • Fill out the fields in the dialog that opens. They are simple and self explanatory. Click Next
  • Choose Web Application (selected by default), enter your site details (or localhost). Done!
  • Now you should see a small table that has all the details that are needed. These include Client ID, Email Address, Client Secret, Redirect URIs, Javascript Origins.
STEP 2: Create the login page
This is the page where you have the big Gmail logo, or Click to login to Gmail link that I had told you about.

Create a page called login.php.

Replace the client id in the code with your client id - the one that you got in the previous step

Create another page called my_redirect.php

Replace the Client Id and the Client Secret with the ones that you got at the end of Step 2. Also, mention the name of the redirect uri with the name of the file - in this case, my_redirect.php. All these are used for validation.

That's about it. You should now be able to run this without any issues. I wrote this to enable developers to directly plug ready baked code and understand the underlying mechanisms later. Very useful when you are running short of time.

I had a requirement of some kind which involves this. So thought this should be shared. I did some searching and found the code and is not written by me. I found it somewhere and had made a copy, and I forgot the original source. In case you find it, please let me know. I thought the entire set of steps will make it easier for others to be able to duplicate this without any issues. In case you want the explanation for the code, please do leave a comment.

Coming soon - How to read Yahoo! contacts using PHP

I wrote this in a great hurry. If something is not working, please let me know ;-)


Popular posts from this blog


The (fake) Quest To Eradicate AIDS with Mythical Mystical Indian roots

Mongoose - An Indian Card Game