Import GMail Contacts Google OAuth Connect with PHP

Jun 06, 2013, by admin

This post explains you how to import Gmail contacts from address book via Google open authorization protocol connect with PHP. It’s simple just few configurations follow below four steps, use this script and give option to invite more friends

Add or register your domain at click here.

Verify your domain ownership with HTML file upload or including META tag.

Google will provide you OAuth consumer key and OAuth consumer secret key.

Download script configurations.

Config.php
You have to change Oauth keys and return URL.

<?php
$consumer_key=’demos.9lessons.info’;
$consumer_secret=’Eeqmv3xxEO2lfA_rM1uVXZ3M’;
$callback=’http://demos.9lessons.info/gmail/Contacts.php’;
$emails_count=’500′; // max-results
?>
GmailConnect.php
Contains PHP code. Connecting to Google for requesting open authentication access tokens.

<?php
include_once ‘GmailOath.php’;
include_once ‘Config.php’;
session_start();
$oauth =new GmailOath($consumer_key, $consumer_secret, $argarray, $debug, $callback);
$getcontact=new GmailGetContacts();
$access_token=$getcontact->get_request_token($oauth, false, true, true);
$_SESSION[‘oauth_token’]=$access_token[‘oauth_token’];
$_SESSION[‘oauth_token_secret’]=$access_token[‘oauth_token_secret’];
?>

<a href=”https://www.google.com/accounts/OAuthAuthorizeToken?oauth_token=<?php echo $oauth->rfc3986_decode($access_token[‘oauth_token’]) ?>“>
<img src=’Googleconnect.png’/>
</a>
Contacts.php
Google return to Contact.php file with contacts data.

<?php
include_once ‘GmailOath.php’;
include_once ‘Config.php’;
session_start();
$oauth =new GmailOath($consumer_key, $consumer_secret, $argarray, $debug, $callback);
$getcontact_access=new GmailGetContacts();
$request_token=$oauth->rfc3986_decode($_GET[‘oauth_token’]);
$request_token_secret=$oauth->rfc3986_decode($_SESSION[‘oauth_token_secret’]);
$oauth_verifier= $oauth->rfc3986_decode($_GET[‘oauth_verifier’]);
$contact_access = $getcontact_access->get_access_token($oauth,$request_token, $request_token_secret,$oauth_verifier, false, true, true);
$access_token=$oauth->rfc3986_decode($contact_access[‘oauth_token’]);
$access_token_secret=$oauth->rfc3986_decode($contact_access[‘oauth_token_secret’]);
$contacts= $getcontact_access->GetContacts($oauth, $access_token, $access_token_secret, false, true,$emails_count);

//Email Contacts
foreach($contacts as $k => $a)
{
$final = end($contacts[$k]);
foreach($final as $email)
{
echo $email[“address”] .”<br />”;
}
}?>

Click Here To Download Script

Click Here To See Like Demo

*The Theme And script were taken from 9lessons.info