Google Calendar API server side access

2019-06-09 18:56发布

问题:

I implemented an utility to perform background operation using Google Calendar API and server side authentication flow without user interface. Now I would like to read events from a private calendar without using authorization flow with offline access or put calendar public.

I'm using this code for authentication:

GoogleCredential credential = new GoogleCredential.Builder()
            .setTransport(SchedulerCalendar.HTTP_TRANSPORT)
            .setJsonFactory(SchedulerCalendar.JSON_FACTORY)
            .setServiceAccountId(SchedulerCalendar.SERVICE_ACCOUNT_EMAIL)
            .setServiceAccountScopes(SchedulerCalendar.SCOPES)
            .setServiceAccountPrivateKeyFromP12File(new File("key.p12")).build();

And the following for read calendar events:

List eventslist = calendar.events().list(calendarid);

The response is 404 for calendar not found.

Any suggestion?

回答1:

I solved my problem sharing the calendar to the *@developer.gserviceaccount.com, and I added http://www.google.com/calendar/feeds/ to scope of the request.

The calendar that I'm using is also from a google apps user so I add also my account *.apps.googleusercontent.com -> http://www.google.com/calendar/feeds/ to Manage API client access from google apps control panel.