[英]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.