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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Join us today at 1 PM EST: "3-Step Approach to Comprehensive Runtime Application Security"
Save your seat
  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

Vinod Chandru user avatar by
Vinod Chandru
·
Mar. 13, 19 · Tutorial
Like (2)
Save
Tweet
Share
11.78K 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": "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.

Google Calendar API

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Choosing the Best Cloud Provider for Hosting DevOps Tools
  • Memory Debugging: A Deep Level of Insight
  • Why Does DevOps Recommend Shift-Left Testing Principles?
  • The Role of Data Governance in Data Strategy: Part II

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: