Posts tagged with oAuth

oAuth on App Engine, Part 2

May 24th, 2009

In my previous post I described how to use Google’s federated login to get an oAuth access token. Now that we’ve stored our access token, we’re going to want to use it in future requests to access the user’s data. Here’s how:

# set up service
gdata_service = gdata.service.GDataService()
gdata.alt.appengine.run_on_appengine(gdata_service)
gdata_service.SetOAuthInputParameters(gdata.auth.OAuthSignatureMethod.HMAC_SHA1, settings.GOOGLE_CONSUMER_KEY, settings.GOOGLE_CONSUMER_SECRET)

# build access_token object and signed request
scopes = [settings.GOOGLE_ANALYTICS_ACCOUNT_URI, settings.GOOGLE_ANALYTICS_DATA_URI]
oauth_input_params = gdata.auth.OAuthInputParams(gdata.auth.OAuthSignatureMethod.HMAC_SHA1, settings.GOOGLE_CONSUMER_KEY, settings.GOOGLE_CONSUMER_SECRET)
access_token = gdata.auth.OAuthToken(scopes=scopes, oauth_input_params=oauth_input_params)
access_token.set_token_string(person.accessToken)
gdata_service.current_token = access_token

# request feed
feed = data_service.GetFeed(settings.GOOGLE_ANALYTICS_ACCOUNT_URI)

feed is a GDataFeed object which can easily be iterated over, like so:

for entry in feed.entry:
    print entry.title.text

To be honest, I’m not sure if the scopes are necessary but I included them for the sake of completeness. Setting oauth_input_params for the oAuth token is essential and something that took me a while to figure out – I had assumed that setting the parameters for the Gdata service would be enough.

I am directly assigning the access token to the Gdata service. I had originally tried to use the setter – gdata_service.SetOAuthToken(access_token) – but had problems and switched to direct assignment. However, my problem could very well have been somewhere else, so feel free to try using the method.

Finally, you may have noticed that I’m querying the Analytics Data API. I hope to have more to announce about that soon. In the meantime, I’ll note that gdata-python-client doesn’t have specific Analytics support, which is why I’m using the generic Gdata service.

OpenID and oAuth on App Engine

May 11th, 2009

Building on my previous post, here are some things I learned today while working to get OpenID and oAuth playing nicely with Django on App Engine.

While App Engine has a very nice login system that hooks seamlessly into Google Accounts, gaining access to the user’s data via one of the Gdata feeds requires an additional authorization (you would use oAuth of course). However, Google has also introduced a federated login method whereby you can send the user to one screen where they both login and approve your access via oAuth to their data. So, I gave up development speed in favor of something that is hopefully simpler for my users. If you want to know more, Joseph Smarr of Plaxo has a good writeup of how the hybrid OpenID + oAuth combination came about. Read more »