4

I want to get GMail contact list in my website using PHP.

And tutorial I referred Here

CODE:

 <?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 = '564766218700- 
 fgtj5fba9h15g8na4khdho1uavo0rtjq.apps.googleusercontent.com';
 $sClientSecret = 'GldOKp2S2ABdp-7owp3ZO_cE';
 $sCallback = 'http://localhost/GmailContact/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('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 ?>

I am encountering with an error such as:

OAuth token parameter missing. That’s all we know.

Questions: 1. How to get OAuth access token?

Please help me.

Rubén
  • 34,714
  • 9
  • 70
  • 166
Aj Kumar
  • 105
  • 2
  • 12
  • Check this link for oauth PHP client http://25labs.com/import-gmail-or-google-contacts-using-google-contacts-data-api-3-0-and-oauth-2-0-in-php/ – SGC Dec 03 '14 at 18:07
  • @SGC i used the above specified tutorial.But mine email addresses are displaying blank. – Aj Kumar Dec 04 '14 at 06:39
  • @SGC echo($xmlrespose) returns:usageLimits accessNotConfigured Access Not Configured. The API is not enabled for your project, or there is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your configuration. https://console.developers.google.com. – Aj Kumar Dec 04 '14 at 10:32
  • 1
    You need to enable the Contacts API in your google console for your project. Is an option for you use paid importers, like [CloudSponge](http://cloudsponge.com/contact-importers/gmail)? – Rael Gugelmin Cunha Dec 04 '14 at 11:28
  • @RaelGugelminCunha it wotked fine!!Ty – Aj Kumar Dec 04 '14 at 12:32
  • @AjKumar Can I create an answer with that, in order you can mark as the correct answer? – Rael Gugelmin Cunha Dec 04 '14 at 16:18

1 Answers1

0

There are several ways to make access token request, and they vary based on the type of application you are building.

For example, a JavaScript application might request an access token using a browser redirect to Google, while an application installed on a device that has no browser uses web service requests.

Some requests require an authentication step where the user logs in with their Google account. After logging in, the user is asked whether they are willing to grant the permissions that your application is requesting. This process is called user consent.

If the user grants the permission, the Google Authorization Server sends your application an access token (or an authorization code that your application can use to obtain an access token). If the user does not grant the permission, the server returns an error.

Here is link for oauth play ground which helps to understand the concepts of Oauth.

Also, check this link for contacts API.

SGC
  • 1,025
  • 1
  • 6
  • 6