Working with Events Using the Google Calendar Service Client in Java

CalendarClient from the com.aspose.email.services.google.calendar.client package works with calendars and events through the Google Calendar API. The client connects over HTTP. To learn how to select an HTTP library, see HTTP Clients for Mail Services.

Connecting to Google Calendar

The client authenticates with OAuth 2.0 in the same way as GmailClient. For details, see Connecting to Gmail.

The following code sample creates a client and lists the calendars of the user. Other code samples on this page use the calendar variable created here.

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());
    }
}

In other methods, use the identifier of a calendar from this list, or primary for the main calendar of the user.

Listing Events

To list events in a period, pass its start and end in the RFC 3339 format to list. The client returns each occurrence of a recurring event as a separate event, sorted by start time.

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());
}

The list method returns one page of results. To read all of them, use 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);

To find events by text, use search:

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

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

Creating an Event

Create a GoogleEvent object, set its properties, and then pass it to create. The start and the end of an event are GoogleEventDateTime objects, which contain a date and a time in the RFC 3339 format and a time zone:

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());

To create an all-day event, use GoogleEventDateTime.allDay. The end date of such an event is exclusive:

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);

Working with Recurring Events

To create a recurring event, set its recurrence rules. To get the occurrences of the event in a period, call 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());
}

To list the recurring events themselves instead of their occurrences, use listMasters.

Updating and Deleting an Event

The update method replaces the event with the object you pass, so get the event first if you want to keep its other properties. The patch method changes only the properties that you set. The delete method removes the event.

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();

Handling Errors

When the Google Calendar API returns an error status, the client throws HttpClientException, which contains the status and the response body. If the token provider cannot get an access token, the client throws java.io.IOException, which is also the base class of HttpClientException.

import com.aspose.ext.http.HttpClientException;

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