Google Calendar API: Using ColdFusion to get a list of Calendars
Before we handle the methods for editing a calendar, we need to make sure we’re editing the correct calendar. To do that we need to get a list of the calendars from Google. This method we’ll look at today is very simple. So, let’s jump right into the code.
<cfargument name="subSessionToken" type="string" required="Yes">
<cfargument name="gsessionid" type="string" required="Yes">
<cfargument name="includeAllCalendars" type="boolean" default="true">
<cfset var local=structnew()>
<cfif arguments.includeAllCalendars>
<cfset local.whichCalendars="allcalendars">
<cfelse>
<cfset local.whichCalendars="owncalendars">
</cfif>
<cfhttp url="http://www.google.com/calendar/feeds/default/#local.whichCalendars#/full?gsessionid=#arguments.gsessionid#" method="get" redirect=false>
<cfhttpparam type="HEADER" name="Authorization" value="AuthSub token=#arguments.subSessionToken#">
</cfhttp>
<cfxml variable="local.getCalendars"><cfoutput>#cfhttp.filecontent#</cfoutput></cfxml>
<cfreturn local.getCalendars/>
</cffunction>
We start with our usual two login arguments of a “subSessionToken” and “gSessionId”. These tokens tell Google whos calendars to get. I added a third token to this method called “includeAllCalendars”. This argument is a boolean true or false value. If it is set to false it will only include calendars that are ‘owned’ by the user. Whereas true will show all calendars the user has access to. So for example, if a coworker shares her calendar with you, you won’t ‘own’ that calendar, but you’ll have access to it.
The next section is a simple cfhttp GET. The final section is just parsing the XML that Google returns. That’s it, we’re done.
I’ll do another post to examine the data that Google returns, it’s important to understand it. I’ll be back shortly I need to run to the chiropractor. Get it? back… oh forget it.
-Steve Nelson








Steve, I used the google calendar API recently and ran into a problem where it would only return 20-25 events max. No matter what parameters I passed it, I could not get the complete list. Have you run into that? I was just wondering if that was fixed yet. Thanks.
Comment by Steve House — February 1, 2008 @ 12:00 am
You need to set the value of "max-results" to a large number. By default they set it to 25 so you don’t accidentally get an enormous packet.
I’ll post a bunch about events next week. I’m taking this slow because there is a LOT going on with the Google Calendar API.
Comment by Steve Nelson — February 2, 2008 @ 12:00 am
Just a minor code suggestion,when you set the variable local.whichCalendars, you don’t need the else. Before you do the if, set it to "owncalendars" and have the if just be the first part. A bit let code and it does the same thing. In fact, and I know it may seem trite, I would argue it’s a bit more clear that local.whichCalendars will always be "owncalendars" unless that condition is met. but that’s just my two-bits worth….
Comment by Allen — February 13, 2008 @ 12:00 am
Fair enough. If that’s a pet peeve of yours I’ll change it.
Comment by Steve Nelson — February 15, 2008 @ 12:00 am