0

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"

}

1 Answer 1

1

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("[email protected]").execute();

To see the events, I can do this:

Events events = calendarService.events().list( "[email protected]").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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.