Google API – Get contact list

Tutorials

In our new tutorial I am going to tell you about inviting friends. I think that this is the most important part for every website, a key to success. Today I will show you how to create simple and effective Gmail contact importer using OAuth authorization and API. Also, I will tell about obtaining Google API access too.

 

As the first step – lets prepare our own project in Google API console, please open this link and create your project. Then we need goto ‘API Access’ section and click ‘Create an OAuth 2.0 client ID’ button. Now we should fill a name for our new project:

Google Contacts API - step 1

Click next, and, at the second step we should set URL of our destination page:

Google Contacts API - step 2

Finally, we’ve got our Client ID and secret (or – consumer key and secret):

Google Contacts API - step 3

Now – download the source files and lets start coding !


Live Demo

[sociallocker]

download in package

[/sociallocker]


Step 1. PHP

Now, please create empty index.php file and put next code:

index.php

01 <?php
02 // disable warnings
03 if (version_compare(phpversion(), "5.3.0"">=")  == 1)
04   error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
05 else
06   error_reporting(E_ALL & ~E_NOTICE);
07 $sClientId 'YOUR_GOOGLE_CLIENT_ID';
08 $sClientSecret 'YOUR_GOOGLE_CLIENT_SECRET';
09 $sCallback 'https://www.script-tutorials.com/demos/291/index.php'; // callback url, don't forget to change it to your!
10 $iMaxResults = 20; // max results
11 $sStep 'auth'// current step
13 include_once('classes/GmailOath.php');
14 session_start();
15 // prepare new instances of GmailOath  and GmailGetContacts
16 $oAuth new GmailOath($sClientId$sClientSecret$argarray, false, $sCallback);
17 $oGetContacts new GmailGetContacts();
18 if ($_GET && $_GET['oauth_token']) {
19     $sStep 'fetch_contacts'// fetch contacts step
20     // decode request token and secret
21     $sDecodedToken $oAuth->rfc3986_decode($_GET['oauth_token']);
22     $sDecodedTokenSecret $oAuth->rfc3986_decode($_SESSION['oauth_token_secret']);
23     // get 'oauth_verifier'
24     $oAuthVerifier $oAuth->rfc3986_decode($_GET['oauth_verifier']);
25     // prepare access token, decode it, and obtain contact list
26     $oAccessToken $oGetContacts->get_access_token($oAuth$sDecodedToken$sDecodedTokenSecret$oAuthVerifier, false, true, true);
27     $sAccessToken $oAuth->rfc3986_decode($oAccessToken['oauth_token']);
28     $sAccessTokenSecret $oAuth->rfc3986_decode($oAccessToken['oauth_token_secret']);
29     $aContacts $oGetContacts->GetContacts($oAuth$sAccessToken$sAccessTokenSecret, false, true, $iMaxResults);
30     // turn array with contacts into html string
31     $sContacts $sContactName '';
32     foreach($aContacts as $k => $aInfo) {
33         $sContactName end($aInfo['title']);
34         $aLast end($aContacts[$k]);
35         foreach($aLast as $aEmail) {
36             $sContacts .= '<p>' $sContactName '(' $aEmail['address'] . ')</p>';
37         }
38     }
39 else {
40     // prepare access token and set it into session
41     $oRequestToken $oGetContacts->get_request_token($oAuth, false, true, true);
42     $_SESSION['oauth_token'] = $oRequestToken['oauth_token'];
43     $_SESSION['oauth_token_secret'] = $oRequestToken['oauth_token_secret'];
44 }
45 ?>
46 <!DOCTYPE html>
47 <html lang="en" >
48     <head>
49         <meta charset="utf-8" />
50         <title>Google API - Get contact list | Script Tutorials</title>
51         <link href="css/main.css" rel="stylesheet" type="text/css" />
52     </head>
53     <body>
54         <header>
55             <h2>Google API - Get contact list</h2>
56             <a href="https://www.script-tutorials.com/google-api-get-contact-list/" class="stuts">Back to original tutorial on <span>Script Tutorials</span></a>
57         </header>
58         <img src="oauthLogo.png" class="google" alt="google" />
59     <?php if ($sStep == 'auth'): ?>
60         <center>
61         <h1>Step 1. OAuth</h1>
62         <h2>Please click <a href="https://www.google.com/accounts/OAuthAuthorizeToken?oauth_token=<?php echo $oAuth->rfc3986_decode($oRequestToken['oauth_token']) ?>">this link</a> in order to get access token to receive contacts</h2>
63         </center>
64     <?php elseif ($sStep == 'fetch_contacts'): ?>
65         <center>
66         <h1>Step 2. Results</h1>
67         <br />
68         <?= $sContacts ?>
69         </center>
70     <?php endif ?>
71 </body>
72 </html>

As you can see – in the beginning we include ‘GmailOath.php’ library. This library you can download here. Once you have downloaded it – pay attention to the code. As you can see – the main functionality is separated into 2 sections: authorization and fetching of contact list. As usual – I put my comments in this code to better understanding.

When we click authorization button, it will open google authorization page, where we should grant access for our application to get our contact list:

Google Contacts API - step 4


Live Demo

Conclusion

If you have any suggestions about further ideas for articles – you are welcome to share them with us. Good luck in your work!

Rate article