Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Google Calendar api "access_denied" on Service Account

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

like image 784
Stephan Celis Avatar asked Dec 14 '12 16:12

Stephan Celis


People also ask

How do I access Google Calendar API?

Enable API accessIn the API Manager pane, click Library to see the list of available APIs. In the Google Apps APIs list, scroll down to click or search for Calendar API, then on the API page, click Enable.


1 Answers

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: [email protected] 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

like image 64
Stephan Celis Avatar answered Sep 20 '22 07:09

Stephan Celis