google-apigoogle-oauthgoogle-calendar-api

Getting my Google service account's email address when I try getting my calendar's "primary" calendar


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:  myserviceaccount@myproject.iam.gserviceaccount.com
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("mypersonalemailaddress@gmail.com").execute();

And it returned

Summary: mypersonalemailaddress@gmail.com

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"

}

Solution

  • Answering my own question...

    Using a service account and doing this

    CalendarList calendarList = calendarService.calendarList().list().execute();

    only seems to list the service account's non-existent calendar, and NOT any shared calendars. So I don't see any items in the calendarlist.

    BUT, I can access my personal email account's calendar doing the following:

    com.google.api.services.calendar.model.Calendar calendar = calendarService.calendars().get("personalemailaddress@gmail.com").execute();

    To see the events, I can do this:

    Events events = calendarService.events().list( "personalemailaddress@gmail.com").setPageToken( pageToken ).execute();

    Now I just have to figure out how to view my personal email account's OTHER calendars (using the email address returns the primary calendar)