gpt4 book ai didi

服务帐户上的 Java Google Calendar api "access_denied"

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:34:39 24 4
gpt4 key购买 nike

我正在尝试使用 Java Google Calendar api 连接到日历。 java 应用程序使用服务帐户。

我有以下代码:

   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();

在最后一行我得到一个 IOException 消息:

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

我仔细检查了 GoogleCredential 对象的值,它们是正确的。我还添加了 https://www.google.com/calendar/feeds/ , http://www.google.com/calendar/feeds/在我的域控制台中使用应用程序 ID 作为客户端以授权第三方应用程序访问

我是不是忘记了一步?

最佳答案

api 还没有完成。更具体地说是服务帐户部分。

日历所有者需要授予应用程序在其日历设置中读取/写入日历的权限。它位于日历的共享设置中,您可以在其中添加帐户的电子邮件地址并授予他们在您的日历上的权限。

因此在这种情况下,我必须将:xxx@developer.gserviceaccount.com 添加到应用程序需要访问的日历的权限/共享列表中。

由于上述问题,我还删除了另一篇未完成的帖子。我将取消删除它,因为它包含一些代码片段,将来可能会对其他人有所帮助。但请注意权限问题和不支持 Google 日历的服务帐户

关于服务帐户上的 Java Google Calendar api "access_denied",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13882162/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com