authorize google calendar api works on personal ac

2019-01-27 01:18发布

I need to make a Java application that stores calendar events in employees within a Google Domain. I created the application based on a sample I found on http://code.google.com/p/google-api-java-client/wiki/APIs#Calendar_API

Now I wanted to make it work on an account from the google domain and suddenly I'm stuck. I changed the client_secrets.json file with a new one. generated on code.google.com/apis/console for an installed application and now I get a invalid client error:

ex = (com.google.api.client.auth.oauth2.TokenResponseException) com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request { "error" : "unauthorized_client" }

Do I need to do other steps for this? Also it needs to work on all accounts by default within the domain not the single account. Maybe other console? I hope not an other library.

2条回答
闹够了就滚
2楼-- · 2019-01-27 01:37

For future reference I used the following code for the authorizing the Google Calendar

   java.io.File licenseFile = new java.io.File("39790cb51b361f51cab6940d165c6cda4dc60177-privatekey.p12");

   GoogleCredential credential = new GoogleCredential.Builder()

  .setTransport(HTTP_TRANSPORT)
  .setJsonFactory(JSON_FACTORY)
  .setServiceAccountId("xxxxx@developer.gserviceaccount.com")   

  .setServiceAccountUser("USER_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();

Also these links helped a lot about setting up a Service account:

OAuth Google API for Java unable to impersonate user

Can a Google Apps Admin manage users files with Drive SDK?

查看更多
贼婆χ
3楼-- · 2019-01-27 01:55

To have access to all accounts in the domain, you need to use a service account:

https://developers.google.com/accounts/docs/OAuth2ServiceAccount

while this link is specific to Drive API, it should give you some code samples that are easily adaptable to Calendar API v3:

https://developers.google.com/drive/delegation

查看更多
登录 后发表回答