简体   繁体   English

如何使用 Google API 通过 Java 获取 Google 日历 ID

[英]How to get the Google Calendar ID using Google API by Java

I'm working right now with Google Calendar API and I have a problem which I searched a lot to solve it and I didn't find a solution so I am sure that some of you can help me with that.我现在正在使用谷歌日历 API 并且我有一个问题,我搜索了很多来解决它,但我没有找到解决方案,所以我相信你们中的一些人可以帮助我。

I want to get the Google Calendar ID by giving the name of the Calendar (Summary)!我想通过提供日历的名称(摘要)来获取 Google 日历 ID!

I tried to get the Event ID by giving the Event name by executing this method我尝试通过执行此方法提供事件名称来获取事件 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;

    }

Your help will be very useful Thank you very much您的帮助将非常有用 非常感谢

As in order to list events you need the calendar id, i am not sure how you think listing the events will help you find a calendar id.为了列出您需要日历 ID 的事件,我不确定您认为列出事件将如何帮助您找到日历 ID。

There is really no way of listing all of the calendars that a user has access to.确实没有办法列出用户有权访问的所有日历。 The best way to find a calendar id is to go to the google calendar website undersetting for the calendar you will find the calendar id.查找日历 ID 的最佳方法是 go 到谷歌日历网站底部设置日历,您将找到日历 ID。

Another option is to use the Calendarlist.list method this will list all the calendars the user has added to their calendarlist which is the list at the bottom left hands side of the google calendar website.另一种选择是使用 Calendarlist.list 方法,这将列出用户添加到其日历列表中的所有日历,该列表位于谷歌日历网站的左下方。 The calendarlist is not a prefect representation of all the calendars a user has but it is close. calendarlist 不是用户拥有的所有日历的完美表示,但它很接近。 You could do a once you have a lit of all the calendars in the calendrlist you could then search though them locally.一旦你在日历列表中找到了所有日历,你就可以做一个,然后你可以在本地搜索它们。

I fixed and i can now get eventID by giving the name of the Calendar and the name of the event which is created in that's one!我修复了,现在我可以通过给出日历的名称和在其中创建的事件的名称来获取 eventID!

here is the getCalendarId method:这是 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;
}

here is the getEventID method:这是 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;

}

the main method:主要方法:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM