2 Legged OAuth in Python


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 = '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)

9 comments:

Eric (Google) said...

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

Unknown said...

Hi,

i have got the same error message
signature_invalid base_string:GET&https%3A%2F%2Fwww.google.com%2Faccounts%2FOAuthGetRe ,.........

the above code works perfect with twitter but not with google.
did anybody got the request token for google ?

Unknown said...

I really thank the author of this article, the article has great significance for me, it just leads me to do better things for this life. Very meaningful, thank you very much, wish you happy
Mobimatic Review
Mobimatic App Builder
Mobimatic Bonus
Mobimatic Review
Mobimatic App Builder
Mobimatic Bonus
Mobimatic Review
Mobimatic App Builder
Mobimatic Bonus
Mobimatic Review
Mobimatic App Builder
Mobimatic Bonus
Mobimatic Review
Mobimatic App Builder
Mobimatic Bonus
Mobimatic Review
Mobimatic App Builder
Mobimatic Bonus

Ideal Assignment Help said...
This comment has been removed by the author.
augustwalker said...

To fix HP printer driver Windows 10 issue , impair HP Smart Install, run ... of introducing HP printer is simple and won't take in excess of a couple of moments.

meldaresearch said...

The ultimate goal of custom argumentative essay writing services is to provide Argumentative Essay Writing Help and argumentative essay writing tips since argumentative essay structure seekers lack time to complete their college argumentative essays.

3RI Technologies said...

Great article, readable content, and well written. Thanks for sharing.
Python Training in Pune

sumitparashar said...

That's Great. Thanks for sharing us nice stuff. This content is really valuable and knowledgeable. I appreciate your knowledge and your writing skill. Keep writing...
EIS SM Pendrive Classes

iteducationcentre said...

Great Post.
Join Python Course in Pune