Java Google Calendar api "access_denied" on Service Account

google-api, google-api-java-client, google-apps, google-calendar-api, java

Solution

The api isn't finished yet. More specifically the service account part.

The calendar owner needs to give permission to the application to read/write the calendar in it's calendar settings. It's found in the sharing settings of the calendar, there you can add e-mail adresses of accounts and give them permission on your calendar.

So in this case I had to add: xxx@developer.gserviceaccount.com to the permission/share list of the calendars the application needed access to.

I also deleted another post that didn't full work because of the issue above. I'll undelete it since it contains some code fragments that may help other people in the future. But beaware of the permission issues and service accounts not supporting Google Calendar

Problem

I'm trying to connect to a calendar using the Java Google Calendar api. The java application uses a service account. I've the following code: ``` java.io.File licenseFile = new java.io.File("39790cb51b361f51cab6940d165c6cda4dc60177-privatekey.p12"); GoogleCredential credential = new GoogleCredential.Builder() .setTransport(HTTP_TRANSPORT) .setJsonFactory(JSON_FACTORY) .setServiceAccountId("xxx@developer.gserviceaccount.com") .setServiceAccountUser(EMAIL_ADRESS) .setServiceAccountScopes(CalendarScopes.CALENDAR) .setServiceAccountPrivateKeyFromP12File(licenseFile) .build(); client = new com.google.api.services.calendar.Calendar.Builder( HTTP_TRANSPORT, JSON_FACTORY, credential) .setApplicationName("Google Calendar Sync").build(); Calendar calendar = client.calendars().get(EMAIL_ADRESS).execute(); ``` On the last line I get an IOException with the message: ex = (com.google.api.client.auth.oauth2.TokenResponseException) com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request { "error" : "access_denied" } I dubble checked the values for the GoogleCredential object and they are correct. I've also added https://www.google.com/calendar/feeds/, http://www.google.com/calendar/feeds/ in my domain console with the application id as client to authorize third party application access Am I forgetting a step?

Original source