[英]How to get the Google Calendar ID using Google API by Java
我现在正在使用谷歌日历 API 并且我有一个问题,我搜索了很多来解决它,但我没有找到解决方案,所以我相信你们中的一些人可以帮助我。
我想通过提供日历的名称(摘要)来获取 Google 日历 ID!
我尝试通过执行此方法提供事件名称来获取事件 ID
public static String getEventId(String title) throws GeneralSecurityException, IOException {
DateTime now = new DateTime(System.currentTimeMillis());
HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
Calendar service = new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
.setApplicationName("applicationName").build();
Events events = service.events().list("primary").setTimeMin(now).setSingleEvents(true)
.execute();
List<Event> items = events.getItems();
String id = null;
for (int i = 0; i < items.size(); i++) {
if (items.get(i).getSummary().toLowerCase().equals(title.toLowerCase())) {
id = items.get(i).getId();
}
}
return id;
}
您的帮助将非常有用 非常感谢
为了列出您需要日历 ID 的事件,我不确定您认为列出事件将如何帮助您找到日历 ID。
确实没有办法列出用户有权访问的所有日历。 查找日历 ID 的最佳方法是 go 到谷歌日历网站底部设置日历,您将找到日历 ID。
另一种选择是使用 Calendarlist.list 方法,这将列出用户添加到其日历列表中的所有日历,该列表位于谷歌日历网站的左下方。 calendarlist 不是用户拥有的所有日历的完美表示,但它很接近。 一旦你在日历列表中找到了所有日历,你就可以做一个,然后你可以在本地搜索它们。
我修复了,现在我可以通过给出日历的名称和在其中创建的事件的名称来获取 eventID!
这是 getCalendarId 方法:
public static String getCalendarId(String calendarName) throws GeneralSecurityException, IOException {
String calendarID = "There is no like this calendar";
HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
Calendar service = new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
.setApplicationName("applicationName").build();
com.google.api.services.calendar.model.CalendarList calendarList = service.calendarList().list().execute();
List<CalendarListEntry> items = calendarList.getItems();
for (int i = 0; i < items.size(); i++) {
if (items.get(i).getSummary().toLowerCase().equals(calendarName.toLowerCase())) {
calendarID = items.get(i).getId();
}
}
return calendarID;
}
这是 getEventID 方法:
public static String getEventId(String calendarName, String eventName) throws GeneralSecurityException, IOException {
DateTime now = new DateTime(System.currentTimeMillis());
HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
Calendar service = new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
.setApplicationName("applicationName").build();
Events events = service.events().list(getCalendarId(calendarName)).setTimeMin(now).setSingleEvents(true)
.execute();
List<Event> items = events.getItems();
String id = null;
for (int i = 0; i < items.size(); i++) {
if (items.get(i).getSummary().toLowerCase().equals(eventName.toLowerCase())) {
id = items.get(i).getId();
}
}
return id;
}
主要方法:
public static void main(String[] args) throws GeneralSecurityException, IOException {
/**
* Getting the Event ID of John Birthday which is in the Family calendar
*/
String calendarName = "Family";
String eventName = "John Birthday";
System.out.println(getEventId(calendarName, eventName));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.