Paddy, the author of this great Zend extension for OAuth helped me
with some sample code that I was able to tweak around and get it
working.
http://code.google.com/p/oauth-for-php/
Here's my version of the sample code:
_________________________________________________________________
<?php
session_start();
//header("Content-type: text/xml");
require_once 'Zend/Oauth/Consumer.php';
require_once 'Zend/Crypt/Rsa/Key/Private.php';
require_once 'Zend/Gdata/ClientLogin.php';
require_once 'Zend/Gdata.php';
$users_index = $_SESSION['users_index'];
// my rsa key string. when in production, put this string in an
external file and call it using get_file_contents
$test_cert = <<<_CERT
-----BEGIN RSA PRIVATE KEY-----
sadfdsfsdfdsfsdfsdfsdsdfsdfsdfsdfsdfsd
-----END RSA PRIVATE KEY-----
_CERT;
$options = array(
'requestScheme' => Zend_Oauth::REQUEST_SCHEME_QUERYSTRING,
'version' => '1.0',
'signatureMethod' => 'RSA-SHA1',
'callbackUrl' => '
http://www.mydomain.com/act/test', // UPDATE FOR
YOUR DETAILS
'requestTokenUrl' => '
https://www.google.com/accounts/
OAuthGetRequestToken',
'userAuthorisationUrl' => '
https://www.google.com/accounts/
OAuthAuthorizeToken',
'accessTokenUrl' => '
https://www.google.com/accounts/
OAuthGetAccessToken',
'consumerKey' => '
www.gpscabin.com', // UPDATE FOR YOUR DETAILS
'consumerSecret' => new Zend_Crypt_Rsa_Key_Private($test_cert)
);
/**
* Example utilises the Google Contacts API
*/
$consumer = new Zend_Oauth_Consumer($options);
if (!isset($_SESSION['ACCESS_TOKEN_GOOGLE'])) {
if (!empty($_GET)) {
// Google does not like empty POST bodies - switch to GET
$token = $consumer->getAccessToken($_GET,
unserialize($_SESSION['REQUEST_TOKEN_GOOGLE']), Zend_Oauth::GET);
$_SESSION['ACCESS_TOKEN_GOOGLE'] = serialize($token);
} else {
// Default POST works fine here since POST body contains scope
parameter
$token = $consumer->getRequestToken(array('scope'=>'http://
www.google.com/m8/feeds'), Zend_Oauth::GET);
$_SESSION['REQUEST_TOKEN_GOOGLE'] = serialize($token);
$consumer->redirect();
exit;
}
} else {
$token = unserialize($_SESSION['ACCESS_TOKEN_GOOGLE']);
$_SESSION['ACCESS_TOKEN_GOOGLE'] = null;
}
// OAuth Access Token Retreived; Proceed to query Data API
$client = $token->getHttpClient($options);
//$client->setUri('
http://www.google.com/m8/feeds/groups/default/
full');
//$client->setMethod(Zend_Http_Client::GET);
$contact = new Zend_Gdata($client);
$contact_count =0;
$query = new Zend_Gdata_Query('
http://www.google.com/m8/feeds/
contacts/
default/full');
$feed = $contact->getFeed($query);
$name = "";
$email = "";
$db = new db_connect();
$db = $db->db;
foreach($feed as $entry){
$name = $entry->title;
$parts = $entry->getExtensionElements();
foreach($parts as $p){
$element = $p->getDOM();
switch($element->tagName){
case 'email':
$email = $element-
>getAttribute('address');
break;
default:
continue;
}
}
$data = array(
'name' => "$name",
'email' => "$email",
'users_index' => $users_index
);
//insert into DB here
$contact_count++;
}
_____________________________________________________________________
Although the library I got from the Zend incubator had to be tweaked a
little to get it work (may be I got an older version)
Here are the tweaks I made to the library.
This line in Zend/Oauth/Http/UserAuthorization.php
____________________________________________________________________
$this->_consumer->getlocalUrl();
change to
$this->_consumer->getCallbackUrl();
_____________________________________________________________________
Although one funny thing is that it returns only the first 25
contacts, no matter what I request.
I tried adding the parameters to the URL, but nothing works. Any idea
why?
-Prem