如何通过 Java 使用 Google API 获取 Google 日历 ID
How to get the Google Calendar ID using Google API by Java
我现在正在使用 Google 日历 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 的最佳方法是访问日历网站下的 google 日历,您将找到日历 ID。
另一种选择是使用 Calendarlist.list 方法,这将列出用户已添加到其日历列表的所有日历,该列表位于 google 日历网站的左下角。日历列表不是用户拥有的所有日历的完美表示,但它很接近。一旦你在 calendrlist 中拥有所有日历,你就可以做一个,然后你可以在本地搜索它们。
我修复了,现在我可以通过给出日历的名称和其中创建的事件的名称来获取 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));
}
我现在正在使用 Google 日历 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 的最佳方法是访问日历网站下的 google 日历,您将找到日历 ID。
另一种选择是使用 Calendarlist.list 方法,这将列出用户已添加到其日历列表的所有日历,该列表位于 google 日历网站的左下角。日历列表不是用户拥有的所有日历的完美表示,但它很接近。一旦你在 calendrlist 中拥有所有日历,你就可以做一个,然后你可以在本地搜索它们。
我修复了,现在我可以通过给出日历的名称和其中创建的事件的名称来获取 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));
}