简体   繁体   中英

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.

I want to get the Google Calendar ID by giving the name of the Calendar (Summary)!

I tried to get the Event ID by giving the Event name by executing this method

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.

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.

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. The calendarlist is not a prefect representation of all the calendars a user has but it is close. 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!

here is the getCalendarId method:

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:

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));

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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