Google Calendar Java API Integration Part 1

Chirag Bhavsar
3 min readFeb 19, 2021

In this article, I am going to explain how to programmatically integrate Google Calendar and Create Events using Service Accounts. This is the series of Articles. I will also add few more for Attachments and Meet Integration.

I am using Java 1.8+ with following dependencies in my project:

<properties>
<java.version>11</java.version>
<project.http.version>1.23.0</project.http.version>
<project.oauth.version>1.23.0</project.oauth.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties>
... <dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client</artifactId>
<version>1.30.4</version>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-calendar</artifactId>
<version>v3-rev260-1.23.0</version>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-jackson2</artifactId>
<version>${project.http.version}</version>
</dependency>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-jetty</artifactId>
<version>${project.oauth.version}</version>
</dependency>
...

Initialize Credentials:

class CalendarSample {...GoogleCredential credential = 
GoogleCredential.fromStream(
CalendarSample.class.
getClassLoader().
getResourceAsStream("your-filename-for-service-account.json")).createScoped(Arrays.asList(CalendarScopes.CALENDAR,"https://www.googleapis.com/auth/calendar.events")).createDelegated("account_you_want_to_impersonate@yourcompany.com");
...private static Event newEvent() {
Event event = new Event();
event.setSummary("New Event");
Date startDate = new Date();
Date endDate = new Date(startDate.getTime() + 3600000);
DateTime start = new DateTime(startDate, TimeZone.getTimeZone("UTC"));
event.setStart(new EventDateTime().setDateTime(start));
DateTime end = new DateTime(endDate, TimeZone.getTimeZone("UTC"));
event.setEnd(new EventDateTime().setDateTime(end));
EventAttendee[] attendees = new EventAttendee[]{
new EventAttendee().setEmail("person@yourcompay.com"),new EventAttendee().setEmail("otherperson@yourcompany.com")
};
event.setAttendees(Arrays.asList(attendees));
return event;
}
...
Event event = newEvent();
Event result = client.events().insert("c_random_string@group.calendar.google.com", event).execute();
}

Credential

Google Java Library works with two types of Authentication:

  • OAuth — which gets consent from User.
  • Service Account Delegation — which impersonates User on their behalf.

As mentioned earlier, we are discussing Service Account in this article. This is a very powerful method and you can impersonate as any user in your system. You can create Calendar Events, Send email etc. on their behalf. So, you have to be careful about this Service Account Key keeping in safe place.

  • Loading JSON : GoogleCredential class loads JSON key that you downloaded from Google Console. You can try P12 but it’s not recommended by Google and I think it will go away at some point. You can follow this guide for creating service accounts.
  • Scopes : You can define scopes for Calendar, Gmail etc. In this example, I have added Calendar and Events. Please make sure that you are using Events also as added scope for creating Events, otherwise it will not work.
  • createDelegated — this is the most important method, which took quite some time for me since there was no clear example of it on delegation. I found it hard way after exploring API, but when it worked I realized that Service Account itself cannot create events. It needs a User Account to create the events.

You would find that GoogleCredential class is shown as deprecated but don’t worry about that for now. At least, I haven’t done research to find alternate class which can support JSON based key reading. I will update this article when I find an alternate solution.

Event

Creating Calendar Invite is Creating Event in Google Calendar API. It is pretty straightforward API to see how you can specify summary(Title),startDate,endDate and description (Body) on Event.

EventAttendee

EventAttendees are folks that you want to invite in your party. You can set who is the organizer by setting organizer flag on EventAttendee like

EventAttendee organizerAttendee = new EventAttendee();
organizerAttendee.setOrganizer(true);

You will not be able to change Creator using this API though. It will be always the person whom you impersonated as.

This is a sample code for creating event

private Event newEvent(List<String> attendeeEmails, Date startDate, String title, String body, String organizer) {
Event event = new Event();
event.setSummary(title);
Date endDate = new Date(startDate.getTime() + 3600000);
DateTime start = new DateTime(startDate, TimeZone.getTimeZone("UTC"));
event.setStart(new EventDateTime().setDateTime(start));
DateTime end = new DateTime(endDate, TimeZone.getTimeZone("UTC"));
event.setEnd(new EventDateTime().setDateTime(end));
List<EventAttendee> eventAttendees = new ArrayList<>();
EventAttendee organizerAttendee = new EventAttendee();
organizerAttendee.setEmail(organizer);
organizerAttendee.setOrganizer(true);
eventAttendees.add(organizerAttendee);
for (String attendee: attendeeEmails) {
EventAttendee eventAttendee = new EventAttendee();
eventAttendee.setEmail(attendee);
eventAttendees.add(eventAttendee);
}
event.setAttendees(eventAttendees);
event.setDescription(body);
return event;
}

Conclusion

I hope this article is useful to you for providing Google Calendar API Integration. The approach for Authentication will work for any other APIs. You just need to change scopes there. I couldn’t find a solution online which will help put all things together.

References:

--

--