Adding extra parameters to a Gdata request

47 views
Skip to first unread message

Prem Pillai

unread,
Sep 1, 2008, 11:13:21 AM9/1/08
to Google Contacts API
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

Trevor Johns

unread,
Sep 2, 2008, 7:39:54 PM9/2/08
to google-co...@googlegroups.com

Hi Perm,
I see this was also filed in the Zend Framework issue tracker as ZF-4158.

The problem seems to be that Zend_Oauth_Client doesn't preserve query
parameters during a GET request. As a result, the max-results
parameter wasn't ever getting sent to the Google Contacts servers.

I've gone ahead and created a patch to fix this, and I'll go ahead and
work on adding it to Zend_Oauth. In the meantime, you can apply it to
your working copy by running the following command from within your
the root of your Zend Framework incubator directory:

curl http://framework.zend.com/issues/secure/attachment/11513/ZF-4158_v1.patch
| patch -p0

Please let me know if this works for you.

--
Trevor Johns

Prem Pillai

unread,
Sep 3, 2008, 2:23:07 AM9/3/08
to Google Contacts API
Thanks Trevor, this works great. :)
-Prem

On Sep 3, 4:39 am, "Trevor Johns" <tjo...@google.com> wrote:
>     curlhttp://framework.zend.com/issues/secure/attachment/11513/ZF-4158_v1.p...
Reply all
Reply to author
Forward
0 new messages