This sample demonstrates a basic structure that you can use to perform 3-legged OAuth using the Google Data Python client library in Google App Engine. This particular example talks to the Documents List Data API.
Note: The sample is available in two versions, one that signs requests with RSA-SHA1 and another that signs with HMAC-SHA1.
Showing posts with label oauth. Show all posts
Showing posts with label oauth. Show all posts
OAuth in Google App Engine
Posted by
Eric (Google)
on
Monday, April 06, 2009
Labels:
app engine,
DocList,
gdata,
oauth,
python
2 Legged OAuth in Python
Posted by
Eric (Google)
on
Sunday, November 23, 2008
Google Apps Premier/Education administrators can take advantage of 2 legged OAuth to communicate with the Google Data APIs.
If you're using the Google Data Python client library 1.2.3+:
An updated sample is here available here.
Otherwise, if you're stuck with an older version of the library (< 1.2.3), you can use the
Python OAuth library from oauth.net.
import urllib import oauth import gdata.contacts import gdata.contacts.service CONSUMER_KEY = 'yourdomain.com' CONSUMER_SECRET = 'YOUR_CONSUMER_SECRET' CONTACTS_URL = 'http://www.google.com/m8/feeds/contacts/default/full' # Setup 2 legged OAuth consumer based on our admin "credentials" consumer = oauth.OAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET) user = '[email protected]' params = {'max-results': 50, 'xoauth_requestor_id': user} # Construct the request manually and sign it using HMAC-SHA1 # Note: The params dictionary needs to be passed in separately from the base URL request = oauth.OAuthRequest.from_consumer_and_token( consumer, http_method='GET', http_url=CONTACTS_URL, parameters=params) request.sign_request(oauth.OAuthSignatureMethod_HMAC_SHA1(), consumer, None) # See patch @ http://code.google.com/p/oauth/issues/detail?id=31 headers = request.to_header() client = gdata.contacts.service.ContactsService() # Query the user's contacts and print their name & email uri = '%s?%s' % (request.http_url, urllib.urlencode(params)) feed = client.GetFeed(uri, extra_headers=headers, converter=gdata.contacts.ContactsFeedFromString) for entry in feed.entry: print '%s, %s' % (entry.title.text, entry.email[0].address)
Labels:
contacts,
google apps,
oauth,
python,
signed
2 Legged OAuth in PHP
Posted by
Eric (Google)
on
Thursday, November 20, 2008
Google Apps Premier/Education administrators can take advantage of 2 legged OAuth to communicate with the Google Data APIs. This sample makes use of the PHP OAuth library from oauth.net.
<?php require_once('OAuth.php'); // Establish an OAuth consumer based on our admin 'credentials' $CONSUMER_KEY = 'yourdomain.com'; $CONSUMER_SECRET = 'YOUR_CONSUMER_SECRET'; $consumer = new OAuthConsumer($CONSUMER_KEY, $CONSUMER_SECRET, NULL); // Setup OAuth request based our previous credentials and query $user= '[email protected]'; $base_feed = 'http://www.google.com/m8/feeds/contacts/default/full/'; $params = array('max-results' => 10, 'xoauth_requestor_id' => $user); $request = OAuthRequest::from_consumer_and_token($consumer, NULL, 'GET', $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 . '?' . implode_assoc('=', '&', $params); echo send_request($request->get_normalized_http_method(), $url, $request->to_header()); /** * Makes an HTTP request to the specified URL * @param string $http_method The HTTP method (GET, POST, PUT, DELETE) * @param string $url Full URL of the resource to access * @param string $auth_header (optional) Authorization header * @param string $postData (optional) POST/PUT request body * @return string Response body from the server */ 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); 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); return $response; } /** * Joins key:value pairs by inner_glue and each pair together by outer_glue * @param string $inner_glue The HTTP method (GET, POST, PUT, DELETE) * @param string $outer_glue Full URL of the resource to access * @param array $array Associative array of query parameters * @return string Urlencoded string of query parameters */ function implode_assoc($inner_glue, $outer_glue, $array) { $output = array(); foreach($array as $key => $item) { $output[] = $key . $inner_glue . urlencode($item); } return implode($outer_glue, $output); } ?>
Subscribe to:
Posts (Atom)