Ruby GData API, Google Calendar, and redirects by jgn on Thursday, May 6, 2010 in Code

The Ruby GData API provides for accessing a number of Google data sources: Google Apps, Analytics, YouTube, you name it (http://code.google.com/apis/gdata/docs/directory.html).

When you access a Google Calendar that is managed through a Google Apps username (e.g., you@example.com, as opposed to you@gmail.com), the standard lookup feed (example: http://www.google.com/calendar/feeds/default/owncalendars/full) redirects after you access it. (Not the case for calendars owned by gmail.com addresses, it would seem.)

Well, bizarrely, in the Ruby implementation (which you can find here: http://code.google.com/p/gdata-ruby-util/ Ruby 1.9 version here: http://code.google.com/u/hoanga/), redirects are not handled properly.

Handling a redirect is a "must have" to get into the Calendar data for a Google Apps user.

The master code repository is hosted on code.google.com, so since it doesn't provide for submitting patches as easily as Github, I'll just lob a monkeypatch fix here . . .

[code language='ruby'] # Fix for GData::Client::Calendar failure to follow redirects module GData module Client class Calendar def make_request(method, url, body = '', retries = 4) response = super(method, url, body) if response.status_code == 302 and retries > 0 @session_cookie = response.headers['set-cookie'] # original wasn't capturing the redirect location redirect_url = response.headers['location'] return self.make_request(method, redirect_url, body, retries - 1) else return response end end end end end

For Rails, you can put this into a file in config/initializers

comments powered by Disqus