简体   繁体   English

服务帐户上的Java Google Calendar api“access_denied”

[英]Java Google Calendar api “access_denied” on Service Account

I'm trying to connect to a calendar using the Java Google Calendar api. 我正在尝试使用Java Google Calendar api连接到日历。 The java application uses a service account. Java应用程序使用服务帐户。

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: 在最后一行,我收到一条带有消息的IOException:

ex = (com.google.api.client.auth.oauth2.TokenResponseException) com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request { "error" : "access_denied" } 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. 我dubble检查了GoogleCredential对象的值,它们是正确的。 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 我还添加了https://www.google.com/calendar/feeds/http://www.google.com/calendar/feeds/与应用程序ID为我的客户域控制台授权第三方应用访问

Am I forgetting a step? 我忘记了一步吗?

The api isn't finished yet. api尚未完成。 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. 因此,在这种情况下,我必须将:xxx@developer.gserviceaccount.com添加到应用程序需要访问的日历的权限/共享列表中。

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 但要注意不支持Google日历的权限问题和服务帐户

I got it to work 我得到了它的工作

I don't know why but I deleted the line 我不知道为什么,但我删除了该行

.setServiceAccountUser(EMAIL_ADRESS)

Also I added an extra url in the domain scope: 我还在域范围中添加了一个额外的URL:

https://apps-apis.google.com/a/feeds/calendar/resource/#readonly

and deleted a link that wasn't working. 并删除了无效的链接。

Finally I changed the applicationName in my client declaration to the same name as in the api console 最后,我将客户端声明中的applicationName更改为与api控制台中的名称相同的名称

client = new com.google.api.services.calendar.Calendar.Builder(
                        HTTP_TRANSPORT, JSON_FACTORY, credential)
                        .setApplicationName("HolidaySyncs").build();

After these steps it started to work. 完成这些步骤后,它开始起作用。

Also note for future reference after I did this I had the following error: 另请注意,在我这样做之后将来的参考我有以下错误:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "notFound",
    "message": "Not Found"
   }
  ],
  "code": 404,
  "message": "Not Found"
 }
}

I solved this by changing for example: 我通过改变例如:

Event result = client.events().insert(<EMAIL ADRESS>, event).execute();

to

Event result = client.events().insert("primary", event).execute();

First I tought there was something wrong with the google servers, but apparently it goes wrong when you try to link to a calendar ID. 首先,我认为谷歌服务器出了问题,但是当你尝试链接到日历ID时显然会出错。 So linking to "primary" which is the primary calendar of an account works. 因此,链接到作为帐户主日历的“主要”工作。 But according to the documentation it should also work when you refer to a specific calendar ID, where the email address is the primary calendar. 但是根据文档,当您引用特定的日历ID时,它也应该起作用,其中电子邮件地址是主日历。 Probably a bug? 可能是一个错误?

UPDATE: after these code correction I still had issues. 更新:在这些代码更正后,我仍然有问题。 Read the accepted answer for more information. 阅读已接受的答案以获取更多信息。

I do: 我做:

List find = client.events().list(EMAIL_ADRESS);
DateTime timeMin = new DateTime(DateUtil.stringToDate("01/01/2013"));
DateTime timeMax = new DateTime(DateUtil.stringToDate("01/02/2013"));

find.setTimeMin(timeMin);
find.setTimeMax(timeMax);

   try{
       Events events = find.execute();
       int i =0;

       while (true) {
           System.out.println("Page: "+(++i)+": "+events.getItems().size());
         for (Event event : events.getItems()) {
           System.out.println(event.getSummary());
         }
         String pageToken = events.getNextPageToken();
         if (pageToken != null && !pageToken.isEmpty()) {
           events = client.events().list(EMAIL_ADRESS).setPageToken(pageToken).execute();
         } else {
           break;
         }
       }

       }catch(GoogleJsonResponseException e){
           System.out.println(e.getMessage());
//         e.printStackTrace();
       }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java Google联系人API访问服务帐户身份验证 - Java Google Contacts API Access Service Account Authentication Java Google Api [服务帐户] - Java Google Api [Service account] 使用Java API的服务帐户验证Google API - Authenticating Google API with a service account with Java API Google Ads API:使用服务帐号访问 - Google Ads API: Access with service account 带有服务帐户的Google Calendar 1.17 API-未解决的GoogleClient类 - Google Calendar 1.17 API with Service Account - unresolved GoogleClient class 使用 Google 日历 API 和服务帐户创建事件 - Creating events using the Google Calendar API and Service Account 在 Java 中使用服务帐户访问 Google 表格 - Access Google Sheet with service account in Java 获取服务帐户的访问令牌时出错:401 Unauthorized\nPOST https://oauth2.googleapis.com/token; 谷歌日历 API - Error getting access token for service account: 401 Unauthorized\nPOST https://oauth2.googleapis.com/token; Google Calendar API 从Java应用程序访问一个(固定)Google帐户日历 - Access to one (fixed) google account calendar from a java application VK API java sdk account.getProfiles访问被拒绝? - VK API java sdk account.getProfiles access denied?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM