我正在尝试为域中的用户检索日历事件。我具有服务帐户访问权限,但是当我尝试获取特定的用户事件时出现404错误。这里是连接代码:
NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(googleApiServiceAccountId)
.setServiceAccountScopes(Collections.singleton(CalendarScopes.CALENDAR_READONLY))
.setServiceAccountPrivateKey(SecurityUtils.loadPrivateKeyFromKeyStore(
SecurityUtils.getPkcs12KeyStore(),
getClass().getClassLoader().getResourceAsStream(googleApiPrivateKeyPath),
NOTASECRET, PRIVATEKEY, NOTASECRET))
.build();
calendarApi = new Calendar.Builder(httpTransport,
JSON_FACTORY, credential).setApplicationName(getApplicactionName()).build();
事件列表方法:
public List<Event> getCalendarEventsForUserAndDates(String userEmail, Long dateFrom, Long dateTo) {
try {
String pageToken = null;
List<Event> allEvents = Lists.newArrayList();
do {
ArrayMap<String, Object> parameters = new ArrayMap<String, Object>();
parameters.add("xoauth_requestor_id", userEmail);
Calendar.Events.List list = calendarApiBean.getCalendarApi()
.events().list("primary");
list.setTimeMax(new DateTime(dateFrom, 0))
.setTimeMin(new DateTime(dateTo, 0))
.setUnknownKeys(parameters);
Events events = list.setPageToken(pageToken)
.execute();
List<? extends Event> items = events.getItems();
if (items != null) {
allEvents.addAll(items);
}
pageToken = events.getNextPageToken();
} while (pageToken != null);
return allEvents;
} catch (IOException e) {
logger.error("error while retriving calendar events for {} and dates {} {} ", userEmail, dateFrom, dateTo);
logger.error("exception", e);
return Collections.emptyList();
}
}
[当我尝试将xoauth_requestor_id设置为用户的电子邮件并列出“主要”时,我会获得我的服务帐户的日历事件。当我将events()。list()参数更改为用户的电子邮件时,出现以下错误:
com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 Not Found
{
"code" : 404,
"errors" : [ {
"domain" : "global",
"message" : "Not Found",
"reason" : "notFound"
} ],
"message" : "Not Found"
}
感谢您的帮助。
为了解决此问题,我必须将此服务帐户添加到Domain Administration上的授权API客户端。客户名称为服务帐户客户ID,范围为“只读日历”
这由两个不同部分组成
创建服务帐户:
授予权限:
填写您需要的范围并授权服务帐户。即:
https://www.googleapis.com/auth/calendar.readonly,https://www.googleapis.com/auth/gmail.readonly
我测试了我链接到源代码中的Python示例,并且可以确认它可以按预期工作。
来源:https://developers.google.com/identity/protocols/OAuth2ServiceAccounthttps://support.google.com/a/answer/162106?hl=en