Java で Google カレンダー サービス クライアントを使用したイベント操作

CalendarClient から参照してください com.aspose.email.services.google.calendar.client パッケージは Google カレンダー API を介してカレンダーとイベントを操作します。クライアントは HTTP 経由で接続します。HTTP ライブラリの選択方法は、 メールサービス用 HTTP クライアント.

Google カレンダーへの接続

クライアントは OAuth 2.0 で同様に認証します GmailClient。詳細は、 Gmail への接続.

以下のコードサンプルはクライアントを作成し、ユーザーのカレンダーを一覧表示します。ページ内の他のサンプルは、 calendar ここで作成された変数です。

import com.aspose.email.services.google.base.auth.GoogleOAuthTokenProvider;
import com.aspose.email.services.google.calendar.client.CalendarClient;
import com.aspose.email.services.google.calendar.model.GoogleCalendarListEntry;

GoogleOAuthTokenProvider tokens = new GoogleOAuthTokenProvider("client-id", "client-secret", "refresh-token");

try (CalendarClient calendar = CalendarClient.builder().tokenProvider(tokens).build()) {
    for (GoogleCalendarListEntry entry : calendar.calendarList().list()) {
        System.out.println(entry.getId() + ": " + entry.getSummary() + ", primary: " + entry.isPrimary());
    }
}

他のメソッドでは、このリストからカレンダーの識別子を使用するか、 primary ユーザーのメインカレンダー用です。

イベントの一覧取得

期間内のイベントを一覧取得するには、開始と終了を RFC 3339 形式で次に渡します list。クライアントは繰り返しイベントの各発生箇所を個別のイベントとして返し、開始時刻でソートします。

import com.aspose.email.services.google.calendar.model.GoogleEvent;

for (GoogleEvent event : calendar.events("primary").list("2030-01-01T00:00:00Z", "2030-02-01T00:00:00Z")) {
    System.out.println(event.getSummary() + ": " + event.getStart().getDateTime());
}

この list メソッドは結果の 1 ページを返します。すべてを取得するには、次を使用します listPaged:

import com.aspose.email.services.google.base.api.GooglePagedResult;
import com.aspose.email.services.google.calendar.model.GoogleEvent;

String pageToken = null;
do {
    GooglePagedResult<GoogleEvent> page = calendar.events("primary")
            .listPaged("2030-01-01T00:00:00Z", "2030-02-01T00:00:00Z", 100, pageToken);
    for (GoogleEvent event : page.getItems()) {
        System.out.println(event.getSummary());
    }
    pageToken = page.getNextPageToken();
} while (pageToken != null);

テキストでイベントを検索するには、次を使用します search:

import com.aspose.email.services.google.calendar.model.GoogleEvent;

for (GoogleEvent event : calendar.events("primary").search("report")) {
    System.out.println(event.getSummary());
}

イベントの作成

作成する GoogleEvent オブジェクトを作成し、プロパティを設定してから、次に渡します create。イベントの開始と終了は GoogleEventDateTime オブジェクトは RFC 3339 形式の日付と時刻、タイムゾーンを含みます:

import com.aspose.email.services.google.calendar.model.GoogleAttendee;
import com.aspose.email.services.google.calendar.model.GoogleEvent;
import com.aspose.email.services.google.calendar.model.GoogleEventDateTime;
import java.util.Arrays;

GoogleEvent event = new GoogleEvent();
event.setSummary("Weekly meeting");
event.setDescription("Review of the weekly report");
event.setLocation("Room 101");
event.setStart(new GoogleEventDateTime("2030-01-10T10:00:00+02:00", "Europe/Kyiv"));
event.setEnd(new GoogleEventDateTime("2030-01-10T11:00:00+02:00", "Europe/Kyiv"));
event.setAttendees(Arrays.asList(new GoogleAttendee("user@example.com")));

GoogleEvent created = calendar.events("primary").create(event);
System.out.println(created.getId() + ": " + created.getHtmlLink());

終日イベントを作成するには、次を使用します GoogleEventDateTime.allDay。このようなイベントの終了日は除外されます:

import com.aspose.email.services.google.calendar.model.GoogleEvent;
import com.aspose.email.services.google.calendar.model.GoogleEventDateTime;

GoogleEvent event = new GoogleEvent();
event.setSummary("Company holiday");
event.setStart(GoogleEventDateTime.allDay("2030-01-10"));
event.setEnd(GoogleEventDateTime.allDay("2030-01-11"));

calendar.events("primary").create(event);

繰り返しイベントの操作

繰り返しイベントを作成するには、繰り返しルールを設定します。期間内の発生箇所を取得するには、次を呼び出します instances:

import com.aspose.email.services.google.calendar.model.GoogleEvent;
import com.aspose.email.services.google.calendar.model.GoogleEventDateTime;
import java.util.Arrays;

GoogleEvent event = new GoogleEvent();
event.setSummary("Weekly meeting");
event.setStart(new GoogleEventDateTime("2030-01-10T10:00:00+02:00", "Europe/Kyiv"));
event.setEnd(new GoogleEventDateTime("2030-01-10T11:00:00+02:00", "Europe/Kyiv"));
event.setRecurrence(Arrays.asList("RRULE:FREQ=WEEKLY;COUNT=10"));

GoogleEvent series = calendar.events("primary").create(event);

for (GoogleEvent occurrence : calendar.events("primary", series.getId())
        .instances("2030-01-01T00:00:00Z", "2030-03-01T00:00:00Z")) {
    System.out.println(occurrence.getStart().getDateTime());
}

発生箇所ではなく繰り返しイベント自体を一覧表示するには、次を使用します listMasters.

イベントの更新と削除

この update メソッドは渡したオブジェクトでイベント全体を置き換えます。したがって、他のプロパティを保持したい場合は事前にイベントを取得してください。The patch メソッドは設定したプロパティのみを変更します。The delete メソッドはイベントを削除します。

import com.aspose.email.services.google.calendar.model.GoogleEvent;

GoogleEvent event = calendar.events("primary", "event-id").get();
event.setSummary("Updated meeting");
calendar.events("primary", "event-id").update(event);

GoogleEvent changes = new GoogleEvent();
changes.setLocation("Room 202");
calendar.events("primary", "event-id").patch(changes);

calendar.events("primary", "event-id").delete();

エラー処理

Google カレンダー API がエラー状態を返すと、クライアントは例外をスローします HttpClientException、ステータスとレスポンスボディを含みます。トークンプロバイダーがアクセストークンを取得できない場合、クライアントは例外をスローします java.io.IOException、これは次のクラスの基底クラスでもあります HttpClientException.

import com.aspose.ext.http.HttpClientException;

try {
    calendar.events("primary", "unknown-id").get();
} catch (HttpClientException e) {
    System.out.println(e.getStatusCode() + ": " + e.getResponseBody());
}