markitus82
markitus82

Reputation: 523

How to get a single GoogleCalendar CalendarEventEntry by its ID?

I'm trying to retrieve a single CalendarEventEntry from a Google Calendar by the event ID but I can't find the way to do it. It seems that the API doesn't provide a method for this, though it suggests to make a query to the feed calendar using Query. The drawback of this is that the ID is not one of the parameters considered.

I think that a possible way to achieve this would be to get the CalendarEventFeed associated with our calendar and then iterate over the resulting list of events as follows:

CalendarService service = new CalendarService(applicationName);
service.setUserCredentials(userName,password);
CalendarEventFeed myFeed = service.getFeed(feedUrl, CalendarEventFeed.class);
List <CalendarEventEntry> entries =  myFeed.getEntries();

for (CalendarEventEntry e : entries){
    if (e.getId().equals(id)){
       return e;
    }
    }

Do you know any easier and more straight solution to achieve this ??

Thanks in advance!

Upvotes: 2

Views: 1754

Answers (2)

ngonidan
ngonidan

Reputation: 1

The guys question has basis, there was no code to answer this question. Find below the code i used to answer this question. I modified your code slightly

import com.google.gdata.client.*;
import com.google.gdata.client.calendar.*;
import com.google.gdata.data.*;
import com.google.gdata.data.acl.*;
import com.google.gdata.data.calendar.*;
import com.google.gdata.data.extensions.*;
import com.google.gdata.util.*;

import java.net.*;
import java.io.*;
import java.util.List;


public class GetGoogleCalendarEventByID {

public static void main(String[] args) {

    System.out.println("Before Creating CalendarService.");
    CalendarService myService = new CalendarService("exampleCo-exampleApp-1.0");
    System.out.println("After Creating CalendarService.");


    try
    {        
        System.out.println("Going to setUserCredentials.");
        myService.setUserCredentials("[email protected]", "password");        

        URL feedUrl =
          new URL("https://www.google.com/calendar/feeds/[email protected]/private/full");




        CalendarEventFeed myFeed = myService.getFeed(feedUrl, CalendarEventFeed.class);
        List <CalendarEventEntry> entries =  myFeed.getEntries();

        for (CalendarEventEntry e : entries){
            if (e.getId().equalsIgnoreCase("http://www.google.com/calendar/feeds/email%40gmail.com/events/EventIDLastPart")){
               System.out.println("Got the CalendarEventEntry: " + e.getId());
               System.out.println("CalendarEventEntry has text: " + e.getPlainTextContent());
            }
            }              


        System.out.println("Finished getting Event.");        




    }
    catch(Exception e)
    {
      System.out.println("Error : " + e.toString());

    }




}
}

NOTE: feedUrl - That is the Calendar URL you find under the calendar settings

An event id has the format http://www.google.com/calendar/feeds/email%40gmail.com/events/EventIDLastPart

e.g.

http://www.google.com/calendar/feeds/email%40gmail.com/events/ghytrueueyryrgfuur

Hope it helps

Ngonidan

Upvotes: 0

Kees de Kooter
Kees de Kooter

Reputation: 7195

You can get the URL of the event by calling calendarEventEntry.getEditLink().getHref(). It is actually the url of the calendar plus the event id.

Take a look at the Data API Developer Guide for code samples.

Upvotes: 1

Related Questions