# Google Calendar API

**URL:** <https://discuss.rubyonrails.org/t/google-calendar-api/27807>\
**Category:** rubyonrails-talk\
**Created:** [April 10, 2008, 10:15am UTC](https://discuss.rubyonrails.org/t/google-calendar-api/27807 "2008-04-10T10:15:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matt\_Davies](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@Matt\_Davies](https://discuss.rubyonrails.org/u/Matt_Davies)\
**Post date:** [April 10, 2008, 10:15am UTC](https://discuss.rubyonrails.org/t/google-calendar-api/27807/1 "2008-04-10T10:15:25Z")

</div>

Hello everyone

Does anyone have any experience of accessing the Google Calendar API with Rails?

I'm using Ben's page as a starting point.

[http://benjamin.francisoud.googlepages.com/googlecalendar](http://benjamin.francisoud.googlepages.com/googlecalendar)

I've installed the libraries but I can get data, I can add data, good start.

I'm now starting to use the get\_calendars method of the gdata class and I'm having problems.

Namely, this bit of code

require 'googlecalendar' g = GData.new g.login('REPLACE\_WITH\_YOUR\_MAIL@gmail.com', 'REPLACE\_WITH\_YOUR\_PASSWORD') calendar\_list = g.get\_calendars() print calendar\_list

Now I'm getting back a 200 OK message object back, I thought I'd be getting an array of calendars I'm a member of.

Has anyone been down this track?

Any information would be helpful and I'll gladly share my experience of setting this up with anyone else.

---

<div class="post-metadata">

**Author:** ![Matt\_Davies](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@Matt\_Davies](https://discuss.rubyonrails.org/u/Matt_Davies)\
**Post date:** [April 10, 2008, 11:37am UTC](https://discuss.rubyonrails.org/t/google-calendar-api/27807/2 "2008-04-10T11:37:14Z")

</div>

A bit more information, if I output this instead I can see exactly what I'm getting back.

print calendar\_list.inspect

This gives me back

#\<Net::HTTPOK 200 OK readbody=true\>

calendar\_list.each { |key, value| print key + ' = ' + value + "\n" }

gives me back

\<Net::HTTPOK 200 OK readbody=true\>last-modified = Thu, 10 Apr 2008 11:32:57 GMT cache-control = max-age=0, must-revalidate, private connection = Close date = Thu, 10 Apr 2008 11:32:57 GMT content-type = application/atom+xml; charset=UTF-8 server = GFE/1.3 transfer-encoding = chunked

I'm starting to think that the get\_calendars method is not doing what it describes itself doing in it's own logic here

[http://pastie.caboo.se/178486](http://pastie.caboo.se/178486)

Although it's much more likely a syntaxical errror on my behalf 🙂

---

<div class="post-metadata">

**Author:** ![Con](https://avatars.discourse-cdn.com/v4/letter/c/9fc29f/32.png) [@Con](https://discuss.rubyonrails.org/u/Con)\
**Post date:** [April 10, 2008, 12:03pm UTC](https://discuss.rubyonrails.org/t/google-calendar-api/27807/3 "2008-04-10T12:03:44Z")

</div>

Hi, I would recommend trying to invoke the following code in the console:

require ‘googlecalendar’ g = GData.new g.login(‘REPLACE\_WITH\_YOUR\_MAIL@gmail.com’,

‘REPLACE\_WITH\_YOUR\_PASSWORD’) calendar\_list = g.get\_calendars() print calendar\_list

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![Matt\_Davies](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@Matt\_Davies](https://discuss.rubyonrails.org/u/Matt_Davies)\
**Post date:** [April 10, 2008, 12:44pm UTC](https://discuss.rubyonrails.org/t/google-calendar-api/27807/4 "2008-04-10T12:44:27Z")

</div>

Hi Conrad, thanks for getting back to me.

The code you suggested I try is exactly the same code that I pasted into the ticket initially 🙂

I found the problem.

In the get\_calendars() method the redirect\_response was being returned, not the redirect\_data.

Changing that line to return redirect\_data gives me exactly what I was looking for, a list of calendars that I have access to.

Thanks anyway Conrad.

---

<div class="post-metadata">

**Author:** ![Matt\_Davies](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@Matt\_Davies](https://discuss.rubyonrails.org/u/Matt_Davies)\
**Post date:** [April 15, 2008, 7:38am UTC](https://discuss.rubyonrails.org/t/google-calendar-api/27807/5 "2008-04-15T07:38:23Z")

</div>

Ahh

Don't listen to me

There's nothing wrong with the redirect\_response line, I wasn't using the right syntax to get at the data.

print calendar\_list.body

That's the puppy.

---

<div class="post-metadata">

**Author:** ![Matt\_Davies](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@Matt\_Davies](https://discuss.rubyonrails.org/u/Matt_Davies)\
**Post date:** [April 16, 2008, 9:59am UTC](https://discuss.rubyonrails.org/t/google-calendar-api/27807/6 "2008-04-16T09:59:49Z")

</div>

Worked it out in the end.

Here's how you access your GData get\_calendars() data and strip out the ID of each entry(calendar) and the name of each entry and create a hash out of the data to use somewhere else.

require 'hpricot' require 'googlecalendar'

k = GData.new &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;k.login('youraddress@gmail.com', 'yourpassword') &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bcalendar\_list = k.get\_calendars() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;calendar\_list = bcalendar\_list.body &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;doc = Hpricot(bcalendar\_list.body.to\_s) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@cal\_names = Hash.new &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(doc/:entry).each do |item| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@cal\_names[item/:id] = item/:name &nbsp;&nbsp;&nbsp;&nbsp;end
