DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • The Rise of the Unsanctioned Agent Swarm
  • Building Multi-Rail Payment Failover to Stop Latency Spikes and Double Debits
  • GET, POST, PUT, DELETE: Choosing HTTP Verbs in a Real .NET Core Web API, Not Just by the Textbook
  • Delegated Transport, Forked Authorization: Where Chat and AI-Agent Integrations Actually Break

Trending

  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop
  • AI-Era Cybersecurity, Part 1: Mapping Glasswing, Daybreak, Lightwell and the Tools Racing to Find and Fix Bugs
  • Save To Bookmarks/Favorites
  • DZone's Article Types

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Databases
  4. Finding a Time to Meet via the Google Calendar API

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

By 
Vinod Chandru
·
Mar. 13, 19 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
16.4K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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": "[email protected]"
        },
        {
          "id": "[email protected]"
        },
        {
          "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": {
    "[email protected]": {
      "busy": [
        {
          "start": "2019-03-02T15:00:00Z",
          "end": "2019-03-02T20:30:00Z"
        }
      ]
    },
    "[email protected]": {
      "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.

Google Calendar API

Opinions expressed by DZone contributors are their own.

Related

  • The Rise of the Unsanctioned Agent Swarm
  • Building Multi-Rail Payment Failover to Stop Latency Spikes and Double Debits
  • GET, POST, PUT, DELETE: Choosing HTTP Verbs in a Real .NET Core Web API, Not Just by the Textbook
  • Delegated Transport, Forked Authorization: Where Chat and AI-Agent Integrations Actually Break

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook