0
1

[Google Calendar API(V3)] Javaでカレンダーを挿入・更新・削除する

Posted at

Google Calendar API V3(Java)を使用して、
Googleカレンダーをカレンダーを挿入・更新・削除する方法
についてご紹介します。

APIを利用する環境の準備から始める場合や、コードを実行する際は、
⧉[Google Calendar API(V3)] JavaでCalendar APIを使う
を参照ください。

No 目次
1 カレンダーを挿入
1 スコープ
2 実行
3 レスポンスの内容
2 カレンダーを上書き
1 スコープ
2 実行
3 レスポンスの内容
3 カレンダーを更新
1 スコープ
2 実行
3 レスポンスの内容
4 カレンダーを削除
1 スコープ
2 実行
3 レスポンスの内容

1. カレンダーを挿入

他のGoogleカレンダーを自身のカレンダーに挿入します。

挿入するカレンダーは自身がアクセス可能でなければなりません。
挿入元のカレンダーの権限に自身を追加してください。

1.1. スコープ

このAPIを実行するには、以下のスコープを指定してください。

CalendarScopes.CALENDAR

⧉[Google Calendar API(V3)] JavaでCalendar APIを使う(2.2 Driveインスタンスを取得)
でスコープを指定してください。

1.2. 実行

public static void main(String[] args) throws Exception{
    Calendar calendar = getCalendar();
    Calendar.CalendarList calendarList = calendar.calendarList();
    
    CalendarListEntry content = new CalendarListEntry();
    content.setId("挿入するカレンダーID");
    Calendar.CalendarList.Insert insert = calendarList.insert(content);
    
    CalendarListEntry res = insert.execute();
    System.out.println(res);
}

1.2.1. HTTPリクエスト

POST: https://www.googleapis.com/calendar/v3/users/me/calendarList
が実行されます。

1.2.2. クエリパラメータ

Calendar.CalendarList.Insertのsetメソッドにより、クエリパラメータを追加できます。

メソッド 引数 説明
setColorRgbFormat Boolean カレンダーの色(RGB)の書き込みにforegroundColorフィールドとbackgroundColorフィールドを使用するか

1.2.3. リクエストボディ

CalendarListEntryのsetメソッドにより、リクエストボディを追加できます。

メソッド 引数 説明
setId String 【必須】カレンダーID
setColorId String 色のID
setBackgroundColor String 背景色(16進数形式)
setForegroundColor String 前景色(16進数形式)
setDefaultReminders List<EventReminder> 予定の通知
setHidden Boolean 非表示か
setNotificationSettings NotificationSettings 予定の通知(その他の通知)
setSelected Boolean UIにコンテンツを表示するか
setSummaryOverride String 概要

EventReminder(set)

メソッド 引数 説明
setMethod String 通知方法
"email":メール、"popup":通知(UIポップアップ)
setMinutes Integer イベント開始の何分前に通知するか
0〜40320分まで指定可能

NotificationSettings(set)

メソッド 引数 説明
setNotifications List<CalendarNotification> その他の通知

CalendarNotification(set)

メソッド 戻り値 説明
getMethod String 通知方法
"email":メールで通知
getType String 通知タイプ

1.3. レスポンスの内容

CalendarListEntry

メソッド 戻り値 説明
getAccessRole String 認証済みユーザーのカレンダーに対してのアクセスロール
getConferenceProperties ConferenceProperties 会議タイプ
getDefaultReminders List<EventReminder> 予定の通知
getDeleted Boolean 削除済みか
getDescription String 説明
getEtag String Eタグ
getHidden Boolean 非表示か
getId String カレンダーID
getKind String リソースの種類
固定文字列:"calendar#calendarListEntry"
getLocation String カレンダーの地域
getNotificationSettings NotificationSettings 予定の通知(その他の通知)
getPrimary Boolean 認証済みユーザーのメインカレンダーか
getSelected Boolean UIにコンテンツを表示するか
getSummary String 名前
getSummaryOverride String 概要
getTimeZone String タイムゾーン
getColorId String 色のID
getBackgroundColor String 背景色(16進数形式)
getForegroundColor String 前景色(16進数形式)

ConferenceProperties

メソッド 戻り値 説明
getAllowedConferenceSolutionTypes List<String> サポートされている会議タイプ
"hangoutsMeet"(http://meet.google.com)のみ

"eventHangout"、"eventNamedHangout"はサポートが終了しているので取得のみ(設定不可)

EventReminder

メソッド 戻り値 説明
getMethod String 通知方法
"email":メール、"popup":通知(UIポップアップ)
getMinutes Integer イベント開始の何分前に通知するか
0〜40320分まで指定可能

NotificationSettings

メソッド 戻り値 説明
getNotifications List<CalendarNotification> その他の通知

CalendarNotification

メソッド 戻り値 説明
getMethod String 通知方法
"email":メールで通知
getType String 通知タイプ

通知タイプ

文字列 説明
eventChange UI:[予定の変更]
[このカレンダー上の予定が変更された場合]
eventCancellation UI:[予定のキャンセル]
[このカレンダー上の予定がキャンセルされた場合]
eventResponse UI:[予定への返信]
[ゲストがこのカレンダー上の予定に返信した場合]
agenda UI:[今日の予定リスト]
[このカレンダー上の予定の概要が毎日メールで届きます]

アクセスロール

文字列 説明
freeBusyReader 空き時間情報への読み取りアクセス権を付与
reader カレンダーへの読み取り権限を付与
writer カレンダーへの読み取りおよび書き込み権限を付与
owner カレンダーの所有権を提供

2. カレンダーを上書き

Googleカレンダーの指定したカレンダーを上書きします。

3. カレンダーを更新と違うところは、
指定した存在するカレンダーを新しいカレンダーに置き換えます。

2.1. スコープ

このAPIを実行するには、以下のスコープを指定してください。

CalendarScopes.CALENDAR

⧉[Google Calendar API(V3)] JavaでCalendar APIを使う(2.2 Driveインスタンスを取得)
でスコープを指定してください。

2.2. 実行

public static void main(String[] args) throws Exception{
    Calendar calendar = getCalendar();
    Calendar.CalendarList calendarList = calendar.calendarList();
    
    CalendarListEntry content = new CalendarListEntry();
    Calendar.CalendarList.Update update = calendarList.update("カレンダーID",content);
    
    CalendarListEntry res = update.execute();
    System.out.println(res);
}

2.2.1. HTTPリクエスト

PUT: https://www.googleapis.com/calendar/v3/users/me/calendarList/{カレンダーID}
が実行されます。

2.2.2. クエリパラメータ

Calendar.CalendarList.Updateのsetメソッドにより、クエリパラメータを追加できます。

メソッド 引数 説明
setColorRgbFormat Boolean カレンダーの色(RGB)の書き込みにforegroundColorフィールドとbackgroundColorフィールドを使用するか

2.2.3. リクエストボディ

CalendarListEntryのsetメソッドにより、リクエストボディを追加できます。

1.2.3. リクエストボディと同じです。

2.3. レスポンスの内容

CalendarListEntry

1.3. レスポンスの内容(CalendarListEntry)と同じです。

3. カレンダーを更新

Googleカレンダーの指定したカレンダーを更新します。

2. カレンダーを上書きと違うところは、
指定した存在するカレンダーを更新します。

3.1. スコープ

このAPIを実行するには、以下のスコープを指定してください。

CalendarScopes.CALENDAR

⧉[Google Calendar API(V3)] JavaでCalendar APIを使う(2.2 Driveインスタンスを取得)
でスコープを指定してください。

3.2. 実行

public static void main(String[] args) throws Exception{
    Calendar calendar = getCalendar();   
    Calendar.CalendarList calendarList = calendar.calendarList();
    
    CalendarListEntry content = new CalendarListEntry();
    Calendar.CalendarList.Patch patch = calendarList.patch("カレンダーID",content);
    
    CalendarListEntry res = patch.execute();
    System.out.println(res);
}

3.2.1. HTTPリクエスト

PATCH: https://www.googleapis.com/calendar/v3/users/me/calendarList/{カレンダーID}
が実行されます。

3.2.2. クエリパラメータ

Calendar.CalendarList.Patchのsetメソッドにより、クエリパラメータを追加できます。

メソッド 引数 説明
setColorRgbFormat Boolean カレンダーの色(RGB)の書き込みにforegroundColorフィールドとbackgroundColorフィールドを使用するか

3.2.3. リクエストボディ

CalendarListEntryのsetメソッドにより、リクエストボディを追加できます。

1.2.3. リクエストボディと同じです。

3.3. レスポンスの内容

CalendarListEntry

1.3. レスポンスの内容(CalendarListEntry)と同じです。

4. カレンダーを削除

Googleカレンダーの指定したカレンダーを削除します。

4.1. スコープ

このAPIを実行するには、以下のスコープを指定してください。

CalendarScopes.CALENDAR

⧉[Google Calendar API(V3)] JavaでCalendar APIを使う(2.2 Driveインスタンスを取得)
でスコープを指定してください。

4.2. 実行

public static void main(String[] args) throws Exception{
    Calendar calendar = getCalendar();
    Calendar.CalendarList calendarList = calendar.calendarList();
    
    Calendar.CalendarList.Delete delete = calendarList.delete("カレンダーID");
    
    delete.execute();
}

4.2.1. HTTPリクエスト

DELETE: https://www.googleapis.com/calendar/v3/users/me/calendarList/{カレンダーID}
が実行されます。

4.2.2. クエリパラメータ

クエリパラメータはありません。

4.2.3. リクエストボディ

リクエスボディはありません。

4.3. レスポンスの内容

レスポンスはありません。
削除に失敗した場合、例外が発生します。



おしまい。。
0
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
0
1