Tips & Tricks 21-Sep-2016

Attach Google Drive files to Calendar events with the Calendar API

The Google Calendar API allows you to create and modify events on Google Calendar. Starting today, you can use the API to also attach Google Drive files to Calendar events to make them—and your app—even more useful and integrated. With the API, you can easily attach meeting notes or add PDFs of booking confirmations to events.

Here's how you set it up:

1) Get the file information from Google Drive (e.g. via the Google Drive API):

GET https://www.googleapis.com/drive/v2/files

{
 ...
 "items": [
  {
   "kind": "drive#file",
   "id": "9oNKwQI7dkW-xHJ3eRvTO6Cp92obxs1kJsZLFRGFMz9Q,
   ...
   "alternateLink": "https://docs.google.com/presentation/d/9oNKwQI7dkW-xHJ3eRvTO6Cp92obxs1kJsZLFRGFMz9Q/edit?usp=drivesdk",
   "title": "Workout plan",
   "mimeType": "application/vnd.google-apps.presentation",
   ...
  },
  ...
 ]
}

2) Pass this information into an event modification operation using the Calendar API:

POST https://www.googleapis.com/calendar/v3/calendars/primary/events?supportsAttachments=true

{
  "summary": "Workout",
  "start": { ... },
  "end": { ... },
  ...
  "attachments": [
   {
      "fileUrl": "https://docs.google.com/presentation/d/9oNKwQI7dkW-xHJ3eRvTO6Cp92obxs1kJsZLFRGFMz9Q/edit?usp=drivesdk",
      "title": "Workout plan",
      "mimeType": "application/vnd.google-apps.presentation"
   },
   ...
  ]
}

Voilà!

You don’t need to do anything special in order to see the existing attachments - they are now always exposed as part of an event:

GET https://www.googleapis.com/calendar/v3/calendars/primary/events/ja58khmqndmulcongdge9uekm7

{
 "kind": "calendar#event",
 "id": "ja58khmqndmulcongdge9uekm7",
 "summary": "Workout",
 ...
 "attachments": [
  {
   "fileUrl": "https://docs.google.com/presentation/d/9oNKwQI7dkW-xHJ3eRvTO6Cp92obxs1kJsZLFRGFMz9Q/edit?usp=drivesdk",
   "title": "Workout plan",
   "mimeType": "application/vnd.google-apps.presentation",
   "iconLink": "https://ssl.gstatic.com/docs/doclist/images/icon_11_presentation_list.png"
  },
  ...
 ]
}

Check out the guide and reference in the Google Calendar API documentation for additional details.

For any questions related to attachments or any other Calendar API features you can reach out to us on StackOverflow.com, using the tag #google-calendar.