Description
I have synced the user calendar event's using my service account. For doing so, I have done below mentioned steps:
- I have added the ACL in user's calendar using their OAuth token. Giving my service account a write permission.
- Then I have fetched the user calendar event using my service account credential.json file.
This flow works perfectly fine for the user personal google account. But the issue arrises when I try to sync the calendar event from the user organisational calendar.
I fined out the issue was accessRole being FreeBusyUser for organisational calendar, preventing me to get the title for that event.
Code I have used:
For adding ACL in user's calendar:
private static final String WRITER_ACCESS = "writer";
private static final String ACL_SCOPE_TYPE = "user";
@Override
public void syncUsersCalendar(String userId, String calId, AccessFilter accessFilter) {
try {
var accessToken = getAccessTokenFromAccessFilter(userId, accessFilter);
var calendarService = CalendarFactory.getCalendarByAccessToken(CLIENT_ID, CLIENT_SECRET, accessToken);
AclRule rule = new AclRule();
rule.setScope(new AclRule.Scope().setType(ACL_SCOPE_TYPE).setValue(serviceAccountEmail));
rule.setRole(WRITER_ACCESS);
calendarService.acl().insert(calId, rule).execute();
} catch (IOException e) {
log.error("Error occurred while syncing calendar", e);
throw new SyncFailedException("Cannot sync the user's calendar to service account.");
}
}
For syncing the calendar event:
var googleEvents = googleCalendar.events()
.list(calendarId).
.setTimeMin(new DateTime(fromDate))
.setTimeMax(new DateTime(toDate))
.setPageToken(null)
.execute();
I have created a bean for calendarService as given below:
@Bean
@Named("serviceAccCalendarService")
public Calendar getServiceAccountCalendarBean() throws IOException, GeneralSecurityException {
InputStream in = CalendarFactory.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
if (in == null) {
throw new IOException("Resource not found: " + CREDENTIALS_FILE_PATH);
}
GoogleCredential credential = GoogleCredential.fromStream(in)
.createScoped(Arrays.asList(
CalendarScopes.CALENDAR,
CalendarScopes.CALENDAR_EVENTS,
CalendarScopes.CALENDAR_SETTINGS_READONLY));
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
return new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();
}
When I check in my organisational calendar I find the ACL writer for my service account. Even though while getting the event I get the access role FreeBusyUser and I am unable to see a title for the synced calendar events.
Can anyone help me solve this issue. Is there any thing that I might be missing here ?