我正在尝试为域中的用户重试日历事件。我有服务帐户访问权限,但在尝试获取特定用户事件时出现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设置为用户的电子邮件并列出“primary”时,我会收到服务帐户的日历事件。当我将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"
}
感谢您的帮助。
答案 0 :(得分:3)
要解决此问题,我必须将此服务帐户添加到域管理上的授权API客户端。 客户端名称是服务帐户客户端ID,范围适用于只读日历
答案 1 :(得分:1)
这由两个不同的部分组成
创建服务帐户:
授予权限:
填写所需的范围并授权服务帐户。即:
https://www.googleapis.com/auth/calendar.readonly,https://www.googleapis.com/auth/gmail.readonly
我测试了我在源代码中链接的Python示例,并且可以确认它可以按预期工作。
来源: https://developers.google.com/identity/protocols/OAuth2ServiceAccount https://support.google.com/a/answer/162106?hl=en