Google API – Get Contact List
Join the DZone community and get the full member experience.
Join For Free
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:
click next, and, at the second step we should set url of our destination page:
finally, we’ve got our client id and secret (or – consumer key and secret):
now – download the source files and lets start coding!
live demo
download in package
now, please create empty index.php file and put next code:
index.php
<?php // disable warnings if (version_compare(phpversion(), "5.3.0", ">=") == 1) error_reporting(e_all & ~e_notice & ~e_deprecated); else error_reporting(e_all & ~e_notice); $sclientid = 'your_google_client_id'; $sclientsecret = 'your_google_client_secret'; $scallback = 'http://www.script-tutorials.com/demos/291/index.php'; // callback url, don't forget to change it to your! $imaxresults = 20; // max results $sstep = 'auth'; // current step // include gmailoath library https://code.google.com/p/rspsms/source/browse/trunk/system/plugins/gmailcontacts/gmailoath.php?r=11 include_once('classes/gmailoath.php'); session_start(); // prepare new instances of gmailoath and gmailgetcontacts $oauth = new gmailoath($sclientid, $sclientsecret, $argarray, false, $scallback); $ogetcontacts = new gmailgetcontacts(); if ($_get && $_get['oauth_token']) { $sstep = 'fetch_contacts'; // fetch contacts step // decode request token and secret $sdecodedtoken = $oauth->rfc3986_decode($_get['oauth_token']); $sdecodedtokensecret = $oauth->rfc3986_decode($_session['oauth_token_secret']); // get 'oauth_verifier' $oauthverifier = $oauth->rfc3986_decode($_get['oauth_verifier']); // prepare access token, decode it, and obtain contact list $oaccesstoken = $ogetcontacts->get_access_token($oauth, $sdecodedtoken, $sdecodedtokensecret, $oauthverifier, false, true, true); $saccesstoken = $oauth->rfc3986_decode($oaccesstoken['oauth_token']); $saccesstokensecret = $oauth->rfc3986_decode($oaccesstoken['oauth_token_secret']); $acontacts = $ogetcontacts->getcontacts($oauth, $saccesstoken, $saccesstokensecret, false, true, $imaxresults); // turn array with contacts into html string $scontacts = $scontactname = ''; foreach($acontacts as $k => $ainfo) { $scontactname = end($ainfo['title']); $alast = end($acontacts[$k]); foreach($alast as $aemail) { $scontacts .= '<p>' . $scontactname . '(' . $aemail['address'] . ')</p>'; } } } else { // prepare access token and set it into session $orequesttoken = $ogetcontacts->get_request_token($oauth, false, true, true); $_session['oauth_token'] = $orequesttoken['oauth_token']; $_session['oauth_token_secret'] = $orequesttoken['oauth_token_secret']; } ?> <!doctype html> <html lang="en" > <head> <meta charset="utf-8" /> <title>google api - get contact list | script tutorials</title> <link href="css/main.css" rel="stylesheet" type="text/css" /> </head> <body> <header> <h2>google api - get contact list</h2> <a href="http://www.script-tutorials.com/google-api-get-contact-list/" class="stuts">back to original tutorial on <span>script tutorials</span></a> </header> <img src="oauthlogo.png" class="google" alt="google" /> <?php if ($sstep == 'auth'): ?> <center> <h1>step 1. oauth</h1> <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> </center> <?php elseif ($sstep == 'fetch_contacts'): ?> <center> <h1>step 2. results</h1> <br /> <?= $scontacts ?> </center> <?php endif ?> </body> </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:
live demo
download in archive
conclusion
if you have any suggestions about further ideas for articles – you are welcome to share them with us. good luck in your work!
Published at DZone with permission of Andrey Prikaznov, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments