Using the following code, when I display the summary of the "primary" calendar, it displays the email address of the service account. I have shared my Google account's calendars with the service account, which, according to Gemini makes them visible to the service account. But, in addition to the strange "primary" calendar summary, it also returns zero calendars when I try to list them (it should show 2 - my primary and my family).
What am I doing wrong?
List< String > scopes = Arrays.asList( "https://www.googleapis.com/auth/calendar.readonly" );
GoogleCredential googleCredential = GoogleCredential
.fromStream(new FileInputStream(SERVICE_ACCOUNT_KEY_PATH))
.createScoped( scopes);
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
Calendar calendarService = new Calendar.Builder(httpTransport, JSON_FACTORY, googleCredential)
.setApplicationName("CalendarListExample")
.build();
com.google.api.services.calendar.model.Calendar calendar = calendarService.calendars().get("primary").execute();
System.out.println( "Summary: " + calendar.getSummary() );
CalendarList calendarList = calendarService.calendarList().list().execute();
if ( calendarList.getItems().size() == 0 ) {
System.out.println( "No calendars" );
}
Output is:
Summary: [email protected]
No calendars
EDIT: I changed the line where I got the "primary" calendar to this (providing my personal email address as posted by someone online):
com.google.api.services.calendar.model.Calendar calendar = calendarService.calendars().get("[email protected]").execute();
And it returned
Summary: [email protected]
EDIT 2: I used my calendar's summary and that actually returned an error. This makes me think both my email address and service account email address is finding SOMETHING. It also contains the correct timezone for my personal email account and the UTC timezone for the service account.
calendarService.calendars().get("mycalendarsummary").execute()
Authentication failed: 404 Not Found
GET https://www.googleapis.com/calendar/v3/calendars/mycalendarsummary
{
"code": 404,
"errors": [
{
"domain": "global",
"message": "Not Found",
"reason": "notFound"
}
],
"message": "Not Found"
}