LoginSignup
1
1

More than 5 years have passed since last update.

Google Calendar API Events: watch できないカレンダーがあるって話

Last updated at Posted at 2019-02-09

@koshi_life です。

TL;DR

Google Calendar API のEvents:watch を用いて
特定カレンダーの変更通知(Push Notification)を受け取ることができますが、Googleが提供している「国の休日」等のカレンダーにはWatch設定できないっていう話です。

公式ドキュメント、Web上にあまり情報がなかったので備忘します。

サンプルスクリプト(Ruby)

CalendarWatcher.rb
class CalendarWatcher

  CALLBACK_URL = 'https://YOUR_SITE'

  # client: Google::Apis::CalendarV3::CalendarService
  # calendar_id: xxx@gmail.com
  def self.start(client, calendar_id)
    # Watchする対象期間は3日後とします。
    expiration = Time.now.strftime('%s%L').to_i + (1000 * 60 * 60 * 24 * 3)

    # Events:watch リクエストパラムの準備
    req = Google::Apis::CalendarV3::Channel.new
    req.id = SecureRandom.uuid
    req.type = 'web_hook'
    req.address = CALLBACK_URL
    req.expiration = expiration.to_s

    # Event:watch コールします。
    client.watch_event(calendar_id, req)
  end

end

Watchできるカレンダー

irb(main):00x:0> email = 'xxx@gmail.com'
irb(main):00x:0> CalendarWatcher.start(client, email)
=> #<Google::Apis::CalendarV3::Channel:xxx @id="d81482a7-300a-4c24-8242-39c213f89f22", @expiration=1552330683000, @kind="api#channel", @resource_id="xxxxxxxxxxxxxxx", @resource_uri="https://www.googleapis.com/calendar/v3/calendars/xxx@gmail.com/events?maxResults=250&alt=json">

Watchできないカレンダー1 「Not Found」

参照権限がないカレンダーに対してWatchリクエストを行った場合は Not Found のエラーとなりました。

irb(main):00x:0> email = 'yyy@gmail.com'
irb(main):00x:0> CalendarWatcher.start(client, email)
Traceback (most recent call last):
        3: from (irb):5
        2: from (irb):5:in `rescue in irb_binding'
        1: from CalendarWatcher.rb:xx: in `start'
Google::Apis::ClientError (notFound: Not Found)

Watchできないカレンダー2 「Push notifications are not supported by this resource.」

irb(main):00x:0> email = 'ja.japanese#holiday@group.v.calendar.google.com'
irb(main):00x:0> CalendarWatcher.start(client, email)
Traceback (most recent call last):
        2: from (irb):3
        1: from
CalendarWatcher.rb:xx: in `start'
Google::Apis::ClientError (pushNotSupportedForRequestedResource: Push notifications are not supported by this resource.)

Push Not Supported For Requested Resource にもあるように、プッシュ通知できないカレンダーリソースということでした。

他にも Google が提供している以下カレンダーにWatch設定することはできませんでした。同じエラーが発生。

  • 日本の祝日: ja.japanese#holiday@group.v.calendar.google.com
  • その年の経過日数: #daynum@group.v.calendar.google.com
  • Los Angeles Dodgers: mlb_19_%4cos+%41ngeles+%44odgers#sports@group.v.calendar.google.com
  • Boston Red Sox: mlb_2_%42oston+%52ed+%53ox#sports@group.v.calendar.google.com

おそらくですが、Googleカレンダーから追加できるGoogle製のカレンダーも同様にWatch設定できないものと見ております。

スクリーンショット 2019-02-09 21.36.49.png

言いたきことは、プッシュ通知の設定ができないカレンダーがあるので、考慮したほうがいいですねっていう話です。

それより、MLBのカレンダーが公開されていることに驚きました。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1