Adobe Solution Partner

February 1, 2008

Google Calendar API: Using ColdFusion to get a list of Calendars

Filed under: ColdFusion, Google — Tags: , , , , — Steve Nelson @ 6:42 am

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.

<cffunction name="getCalendars" >
        <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

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • StumbleUpon
  • Technorati
  • TwitThis

6 Comments »

  1. 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

  2. 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

  3. 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

  4. Fair enough. If that’s a pet peeve of yours I’ll change it.

    Comment by Steve Nelson — February 15, 2008 @ 12:00 am

  5. I know I’m behind times – posting something in 2010 on things that have been here since 2008. But…I cant find a resource for this anywhere. I’ve been trying to create a Google Contact using the Google API with ColdFusion. I seriously don’t get it and was wondering if anyone knows where I can find a code sample to update / or add Contact data. I’m having trouble following Google’s site on the process :( HELP. Thanks in advance.

    Comment by Angel Gonzalez — August 4, 2010 @ 6:58 pm

  6. Have you already gone through the few posts I have here? They walk through the basics of logging in and then downloading the data from google, etc.

    Comment by Steve Nelson — August 4, 2010 @ 10:34 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment

 

Server Down?

Maximize Web application uptime by drawing upon Webapper's years of experience tuning and stabilizing many of the world's largest ColdFusion Web applications. Contact us today!