dackdive's blog

新米webエンジニアによる技術ブログ。JavaScript(React), Salesforce, Python など

Google Calendar APIにDrive添付機能がついたので試してみた

Twitter で流れてきたこちらの Google Developer ブログ記事。

Google Cloud Blog - News, Features and Announcements

Calendar API で予定に Drive ファイルの添付が可能になった、とのこと。
たしかに Google カレンダーを見ると、いつのまにか「添付ファイル」という項目が追加されていた。。。
(ここに記事があった。 Google Workspace Updates: Event attachment improvements for Google Calendar web and API

f:id:dackdive:20150624095231p:plain

また、Google Calendar API の Event Resource の部分を見ても、attachments[] というプロパティが追加されているよう。

(該当プロパティのみ抜粋)

Property name Value Description
attachments[] list File attachments for the event.
Currently only Google Drive attachments are supported.
In order to modify attachments the supportsAttachments request parameter should be set to true.

There can be at most 25 attachments per event,
attachments[].fileUrl string URL link to the attachment.
For adding Google Drive file attachments use the same format as in alternateLink property of the Files resource in the Drive API.
attachments[].iconLink string URL link to the attachment's icon.
Read-only.
attachments[].mimeType string Internet media type (MIME type) of the attachment.
attachments[].title string Attachment title.

というわけで、さっそく API を叩いて試してみる。

試してみる

GoogleAPI を叩いてみるには API リファレンスのページを利用すると便利です。

Drive API

まずは、Drive API の Files: list のリファレンスページ にアクセス。

f:id:dackdive:20150624113515p:plain

一番下の「Try it!」から OAuth2.0 による認証(キャプチャのスライドバー)を有効にした後、 maxResults を適当に 10 とか入力して EXECUTE ボタンで API を叩く。

次のようなレスポンスが返される。

{
 "kind": "drive#fileList",
 "etag": "*********",
 "selfLink": "https://www.googleapis.com/drive/v2/files?maxResults=10",
 "nextPageToken": "**************",
 "nextLink": "https://www.googleapis.com/drive/v2/files?maxResults=10&pageToken=***********",
 "items": [
    /* ここに Drive のファイルの情報が並ぶ */
  ]
}

items の各要素はこんな感じ。プロパティの詳細は Drive API の File の Resource より確認できる。

{
  "kind": "drive#file",
  "id": "*************",
  "etag": "***************",
  "selfLink": "https://www.googleapis.com/drive/v2/files/1O5vm3awMl4kTu_dT8f-rZxh7cyLCR_jGAnRkodhqRlw",
  // ↓ を使う
  "alternateLink": "https://docs.google.com/spreadsheets/d/1O5vm3awMl4kTu_dT8f-rZxh7cyLCR_jGAnRkodhqRlw/edit?usp=drivesdk",
  "embedLink": "https://docs.google.com/spreadsheets/d/1O5vm3awMl4kTu_dT8f-rZxh7cyLCR_jGAnRkodhqRlw/htmlembed",
  ...
  "title": "Calendar API Drive添付用サンプルファイル",
  "mimeType": "application/vnd.google-apps.spreadsheet",
  ...
}

このうち alternateLink というプロパティを使うのでメモっておく。

Calendar API

次に Calendar API を叩いてカレンダーに予定を追加してみる。
Drive API の時と同じようにリファレンスページから実行することが可能だけど、ここで注意。

https://developers.google.com/google-apps/calendar/v3/reference/events/insert ではなく、ブログ記事からリンクされている以下の URL にアクセスする。

https://developers.google.com/google-apps/calendar/create-events?utm_campaign=calendarAPI-615&utm_source=gadbc&utm_medium=blog#attachments

どうも、今回の添付ファイル機能は

https://www.googleapis.com/calendar/v3/calendars/[calendar_id]/events?supportsAttachments=true

というように、URL 末尾に supportsAttachments=true というクエリパラメータをつけて実行する必要があるみたいなんだけども
通常の /events/insert のリファレンスページからだとこのオプションをつけて実行することができない。

あとは Drive の時と同じように「Try it!」から Authorize した後、パラメータを入力して EXECUTE ボタンを押す。

パラメータは以下のように、start, end は 2015/06/24 12:00 - 13:00 (JST)とし、
attachementsfileUrl には先ほど Drive API で取得した alternateLink を入力する。
title は任意。

f:id:dackdive:20150624114055p:plain

実行した結果、Google カレンダーには次のように予定が登録されていた。

f:id:dackdive:20150624113247p:plain

Drive が添付されていますね。
ちなみに、title は添付するファイルのファイル名とは無関係につけることが可能で、省略するとファイルの URL そのものが表示される。

うーん、ますます Google カレンダーが便利になりそう。