I am new to OAuth, and want to create a page that gets the user's contact list from Google using the OAuth system so they don't have to login.
How do I do this? I am using php, so I would really appreciate if there is example code that does this. I can't seem to find it on Google.
Please help!
Thanks
To access personal contacts: https://www.googleapis.com/auth/contacts. To access directory information: https://www.googleapis.com/auth/directory.readonly. Note: The Domain Shared Contacts API is not affected by these changes.
Google APIs use the OAuth 2.0 protocol for authentication and authorization. Google supports common OAuth 2.0 scenarios such as those for web server, client-side, installed, and limited-input device applications. To begin, obtain OAuth 2.0 client credentials from the Google API Console.
For general OAuth principles to access Google, you might find Google's OAuth playground very useful (contacts are included there).
This is a very basic example (using the php oauth pecl extension and simplexml, it just prints out the names of the 25 first contacts):
<?php
$cons_key="your consumer key";
$cons_sec="your consumer secret";
$callback="http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
$req_token_url="https://www.google.com/accounts/OAuthGetRequestToken";
$auth_token_url="https://www.google.com/accounts/OAuthAuthorizeToken";
$acc_token_url="https://www.google.com/accounts/OAuthGetAccessToken";
$scope="https://www.google.com/m8/feeds/";
$scopes=urlencode($scope);
$req_scope_token_url=$req_token_url."?scope=".$scopes;
$endpoint="https://www.google.com/m8/feeds/contacts/default/full/";
session_start();
if(!isset($_GET['oauth_token']) && $_SESSION['state']==1) $_SESSION['state'] = 0;
try {
$oauth = new OAuth($cons_key,$cons_sec);
if(!isset($_GET['oauth_token']) && !$_SESSION['state']) {
$oauth = new OAuth($cons_key,$cons_sec);
$oauth->setRequestEngine(OAUTH_REQENGINE_CURL);
$request_token_info = $oauth->getRequestToken($req_scope_token_url,$callback);
if(!empty($request_token_info)) {
$_SESSION['token']=$request_token_info['oauth_token'];
$_SESSION['secret']=$request_token_info['oauth_token_secret'];
$_SESSION['state']=1;
header('Location: '.$auth_token_url.'?oauth_token='.$_SESSION['token']);
exit;
}
} else if($_SESSION['state']==1) {
$oauth->setToken($_GET['oauth_token'],$_SESSION['secret']);
$access_token_info = $oauth->getAccessToken($acc_token_url);
$_SESSION['state'] = 2;
$_SESSION['token'] = $access_token_info['oauth_token'];
$_SESSION['secret'] = $access_token_info['oauth_token_secret'];
}
$oauth->fetch($endpoint);
parseAtom($oauth->getLastResponse());
} catch(OAuthException $E) {
print_r($E);
}
function parseAtom($atomstring) {
global $oauth;
$atom=simplexml_load_string($atomstring);
foreach ($atom->entry as $entry) {
print $entry->title.", ";
}
}
?>
You can see the above code in action here.
Installing (configuring) the oauth pecl extension can be tricky, you may have to check your php.ini and/or specify a requestengine.
Hope my post helpful to all (Google contact list reader in PHP(Google OAuth)) http://anandafit.info/2011/03/08/google-contact-list-reader-in-php-google-oauth/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With