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
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Implementing Secure API Gateways for Microservices Architecture
  • Contract-First Integration: Building Scalable Systems With Flyway, OpenAPI, and Kafka
  • Building a Zero-Cost Approval Workflow With AWS Lambda Durable Functions
  • Programmatic Brand Extraction: Pulling Logos, Colors, and Assets from Any URL

Trending

  • Catching Data Perimeter Drift Before It Reaches Production
  • The Hidden Cost of Overprivileged Tokens: Designing Messaging Platforms That Assume Compromise
  • Dear Micromanager: Your Distrust Has a Job; It’s Just Not the One You’re Doing
  • From AI Chaos to Control: Building Enterprise-Grade LLM Gateways With MuleSoft Anypoint
  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 user avatar
Vinod Chandru
·
Mar. 13, 19 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
16.0K Views

Join the DZone community and get the full member experience.

Join For Free

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

  • Implementing Secure API Gateways for Microservices Architecture
  • Contract-First Integration: Building Scalable Systems With Flyway, OpenAPI, and Kafka
  • Building a Zero-Cost Approval Workflow With AWS Lambda Durable Functions
  • Programmatic Brand Extraction: Pulling Logos, Colors, and Assets from Any URL

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • 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