Saturday, August 31, 2013

Import GMail Contacts Google OAuth Connect with PHP

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.

Step 1

    Add or register your domain at click here.

Step 2

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

Step 3

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

Step 4
Config.php
You have to change Oauth keys and return URL.
<?php
$consumer_key=''demo.com';
$consumer_secret='EO2lVXZ3MEeqmv3xxfA_rM1u';
$callback='http://demo.com/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 />";
    }
}?>
 

No comments:

Post a Comment