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 = 'any.user@yourdomain.com'
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)
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.
Subscribe to:
Post Comments (Atom)
2 comments:
hi there,
when i try to getRequestToken, i get this error:
-------------------------
signature_invalid base_string:GET&https%3A%2F%2Fwww.google.com%2Faccounts%2FOAuthGetRequestToken&oauth_consumer_key%3Dwww.w3norm.com%26oauth_nonce%3D42A1A64CB6AD8EA86BF56A4383A4BBD40F67914B%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1233433987%26oauth_version%3D1.0%26scope%3Dhttps%253A%252F%252Fwww.google.com%252Fm8%252Ffeeds
-------------------------
any idea?
Hi Sinuy,
With 2 Legged OAuth, there's no oauth token, so you don't have to fetch a request token.
An example of 3 Legged OAuth (normal flow) using the Python library is available here:
http://code.google.com/p/gdata-python-client/source/browse/trunk/samples/oauth/oauth_example.py
Feel free to post in the Google Accounts APIs group if you have questions.
http://groups.google.com/group/Google-Accounts-API
Eric
Post a Comment