Import Gmail Contacts using PHP- Google OAuth

Inviting friends is the most important part of the web project success. 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.

Image

Click Here for Live Demo

Step 1

Add or register your domain at click here.

Image

Step 2

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

Image

Step 3

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

Image

Step 4

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&#8217;;
$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 />”;
}
}?>

One thought on “Import Gmail Contacts using PHP- Google OAuth

  1. fantastic issues altogether, you simply won a new reader.
    What would you suggest in regards to your submit that you simply made some
    days ago? Any sure?

Leave a comment