Saturday 17 August 2013

Google API – Get contact list


Google API – Get contact list 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 https://code.google.com/apis/console/  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 button, 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 from Here  and lets start coding

index.php
<?php
  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 Client Secret ';
 $sCallback = 'http://localhost/srinu/googleapi/index.php'; // change it to your call back url!
 $iMaxResults = 90; // max results
 $sStep = 'auth'; // current step

   include_once('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 | Sri info</title>
        
    </head>
    <body>
       <h1> Get Contect List using Google api<h1>
       

    <?php if ($sStep == 'auth'): ?>
        <center>
        <h3>Step 1. OAuth</h3>
        <h4>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</h4>
        </center>
    <?php elseif ($sStep == 'fetch_contacts'): ?>
        <center>
        <h2>Step 2. Results</h2>
       
        <?= $sContacts ?>
        </center>
    <?php endif ?>

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

JQuery Slider


Drag a handle to select a numeric value.
The basic slider is horizontal and has a single handle that can be moved with the mouse or by using the arrow keys


<html lang="en">
 <head>
  <meta charset="utf-8" />
  <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
   <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
   <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
  <script>
 $(function() {
 $( "#slider-range-max" ).slider({
 range: "max",
 min: 1,
 max: 100,
 value: 1,
 slide: function( event, ui ) {
 $( "#amount" ).val( ui.value );
 }
 });
 $( "#amount" ).val( $( "#slider-range-max" ).slider( "value" ) );
 });
  </script>
 </head>
   <body>
 <p>
    <form name="qnty" action="" method="post">
 <label for="amount">Quantity:</label>
 <input type="text" id="amount" readonly style="border: 0; color: #38761d; font-weight: bold;" name="qnty"/>
 </p>
 <div id="slider-range-max" style="cursor:pointer"></div>
 <br>
 <input type="submit" value="submit">
   </body>
  </form>
 </html>