Using session tokens with google's AuthSub python api

If you’re a little frustrated with the API docs for the gdata python api I’m right there with you. I’m trying to build a django app that accesses Google calendar. The basic “hello world” stuff works fine but when I want to actually use it for my own app I get frustrated. For example, the doc explains how to deal with authsub tokens but doesn’t really tell you about what to do with session tokens. Finally after banging my head on the wall a few times I figured it out.

First you have to get a one-time use authsub token. This is pretty clear from the docs linked above. The tutorial’s example code looks like this (authsub_token is probably set from the ?token=… GET param):

calendar_service = gdata.calendar.service.CalendarService()
calendar_service.auth_token = authsub_token
calendar_service.UpgradeToSessionToken()
feed = calendar_service.GetCalendarListFeed()
for i, a_calendar in enumerate(feed.entry):
print '\t%s. %s' % (i, a_calendar.title.text,)

You probably want to store your upgraded session token in a database so that you can access the feed later. To do that realize that calendar_service.auth_token now contains a new token, different than what was previously in your authsub_token variable. Store this in your database and the next time you instantiate gdata.calendar.service.CalendarService() set auth_token to this stored session token and skip the UpgradeToSessionToken() step. For example, this code will work (session_token is retrieved from your database or wherever you put it):

calendar_service = gdata.calendar.service.CalendarService()
calendar_service.auth_token = session_token
feed = calendar_service.GetCalendarListFeed()
for i, a_calendar in enumerate(feed.entry):
print '\t%s. %s' % (i, a_calendar.title.text,)

I hope this helps make things slightly more clear to someone.

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You can use Markdown syntax to format and style the text.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
2 + 0 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.

Back to top