Hello,
I have the following code which I use for my Google apps domain. It is
writen in 2-legged Oauth and is working without problems.
I am trying to migrate the code to 3-legged Oauth but I am having problems
understanding what I have to change. Do I just change $CONSUMER_SECRET with
the Access Token when requesting data? (tried that, didn't work, but perhaps
I messed up something else)
<?
require_once('oauth.php');
$CONSUMER_KEY = 'myGoogleDomain.com';
$CONSUMER_SECRET = 'myAutoGenKey';
$user = "[email protected]";
$consumer = new OAuthConsumer($CONSUMER_KEY, $CONSUMER_SECRET, NULL);
$base_feed = 'https://www.google.com/m8/feeds/contacts/' . $user . '/full';
$params = array('max-results' => 2000, 'xoauth_requestor_id' => $user);
$xmlStr = execUrl($base_feed,$params,"GET");
private function execUrl($base_feed,$params,$method, $xml=null)
{
global $consumer;
$request = OAuthRequest::from_consumer_and_token($consumer, NULL,
$method, $base_feed, $params);
// Sign the constructed OAuth request using HMAC-SHA1
$request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $consumer,
NULL);
// Make signed OAuth request to the Contacts API server
$url = $base_feed . '?' . $this->implode_assoc('=', '&', $params);
$retArr = $this->send_request($request->get_normalized_http_method(),
$url, $request->to_header(),$xml);
return($retArr);
}
private function send_request($http_method, $url, $auth_header=null,
$postData=null)
{
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HEADER, 1 );
switch($http_method) {
case 'GET':
if ($auth_header) {
curl_setopt($curl, CURLOPT_HTTPHEADER, array($auth_header));
}
break;
case 'POST':
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:
application/atom+xml',
$auth_header));
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
break;
case 'PUT':
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:
application/atom+xml',
$auth_header));
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $http_method);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
break;
case 'DELETE':
curl_setopt($curl, CURLOPT_HTTPHEADER, array($auth_header));
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $http_method);
break;
}
$response = curl_exec($curl);
if (!$response) {
$response = curl_error($curl);
}
curl_close($curl);
$xml = strstr($response,"<");
$header = substr($response,0,strpos($response,"<"));
return array('headers' => $header, 'xml' => $xml);
}
?>
--
You received this message because you are subscribed to the Google
Groups "Google Contacts, Shared Contacts and User Profiles APIs" group.
To post to this group, send email to
[email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://code.google.com/apis/contacts/community/forum.html