Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Finding a Time to Meet via the Google Calendar API

DZone 's Guide to

Finding a Time to Meet via the Google Calendar API

In this post, we’ll describe how to use an API endpoint to duplicate the data Google Calendar displays

· Integration Zone ·
Free Resource

In any organization, users commonly need to see when others are available to meet on a given day or over a certain time period. This information helps schedule meetings and also book resources such as rooms or equipment. Our own organization uses Google Calendar for this purpose, as shown below.

Apps integrating with Google Calendar often have similar needs for this data. The naive way to retrieve this meeting data via the API would be to fetch all the events in a user’s calendar(s), resolve overlapping ones, and use it to determine available times. However, this approach is more complex than it needs to be.

Fortunately, Google Calendar provides an API endpoint (docs) to retrieve free/busy information within a calendar account. “Free/Busy” data represents the times a user’s calendar indicates the user is “busy” and implies when they’re “free.” In this post, we’ll describe how to use this API endpoint to duplicate the data Google Calendar displays in the screenshot above.

Accessing Free/Busy Data

First, we obtain the list of calendar IDs we’d like to query for availability. Google Calendar’s calendarList endpoint helps with this. Alternatively, Google supports the keyword primaryto refer to the currently authenticated user’s primary calendar, and email addresses to refer to other users’ primary calendars.

With the calendar IDs, applications can query for each other’s availability within a certain time range:

curl -X POST -H "Content-Type: application/json" \
    'https://www.googleapis.com/calendar/v3/freeBusy' \
    -H "Authorization: Bearer TOKEN" -d '{
      "items": [
        {
          "id": "kloudless.com_333093430303834363937@resource.calendar.google.com"
        },
        {
          "id": "chris@kloudless.com"
        },
        {
          "id": "primary"
        }
      ],
      "timeMin": "2019-03-02T07:00:00-0800",
      "timeMax": "2019-03-02T21:00:00-0800",
    }'

The response contains a list of time ranges during which each calendar is busy:

{
  "kind": "calendar#freeBusy",
  "timeMin": "2019-03-02T15:00:00.000Z",
  "timeMax": "2019-03-03T05:00:00.000Z",
  "calendars": {
    "kloudless.com_333093430303834363937@resource.calendar.google.com": {
      "busy": [
        {
          "start": "2019-03-02T15:00:00Z",
          "end": "2019-03-02T20:30:00Z"
        }
      ]
    },
    "chris@kloudless.com": {
      "busy": [
        {
          "start": "2019-03-02T15:00:00Z",
          "end": "2019-03-02T20:30:00Z"
        },
        {
          "start": "2019-03-03T02:00:00Z",
          "end": "2019-03-03T03:15:00Z"
        }
      ]
    },
    "primary": {
      "busy": [
        {
          "start": "2019-03-02T21:30:00Z",
          "end": "2019-03-02T23:45:00Z"
        },
        {
          "start": "2019-03-03T01:00:00Z",
          "end": "2019-03-03T02:00:00Z"
        },
        {
          "start": "2019-03-03T03:45:00Z",
          "end": "2019-03-03T05:00:00Z"
        }
      ]
    }
  }
}

Notice how the time ranges in the response are in UTC. If you’d like them in the user’s time zone instead, such as PST based on the request in this case, add in "timeZone": "-0800"to the request object. Also notice that the room resource itself is represented by a calendar. This simplifies handling resources such as rooms.

The data above represents the meetings in each calendar. Apps can use it to determine non-overlapping time ranges during which all meeting participants and resources such as conference rooms are available.

The Google Calendar API endpoint above also supports Google Group identifiers instead of individual calendar IDs. This is very helpful when scheduling an event that includes all the members in a Google Group or mailing list.

To learn more about integrating multiple Google calendars, check out our blog.

Topics:
calendar api ,integration ,google api ,google app for business ,cloud app development ,cloud application development ,cloud application ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}