简体   繁体   中英

How can I use the Google Calendar API for Java to delete an event on my Google Calendar?

I've searched high and low to find an example as what I am trying does not seem to be working. I am getting this error:

com.google.gdata.util.InvalidEntryException: Bad Request
Invalid request URI

    at com.google.gdata.client.http.HttpGDataRequest.handleErrorResponse(HttpGDataRequest.java:594)
    at com.google.gdata.client.http.GoogleGDataRequest.handleErrorResponse(GoogleGDataRequest.java:563)
    at com.google.gdata.client.http.HttpGDataRequest.checkResponse(HttpGDataRequest.java:552)
    at com.google.gdata.client.http.HttpGDataRequest.execute(HttpGDataRequest.java:530)
    at com.google.gdata.client.http.GoogleGDataRequest.execute(GoogleGDataRequest.java:535)
    at com.google.gdata.client.Service.update(Service.java:1563)
    at com.google.gdata.client.Service.update(Service.java:1530)
    at com.google.gdata.client.GoogleService.update(GoogleService.java:583)
    at CalendarConnect.deleteEvent(CalendarConnect.java:37)
    at ProgMain.main(ProgMain.java:26)

Here is an example of the code I am using:

CalendarService service = new CalendarService("Fourth Year Project");
             service.setUserCredentials(username, password);

             URL postUrl = new URL("https://www.google.com/calendar/feeds/default/private/full");
             CalendarEventEntry myEntry = new CalendarEventEntry();

             myEntry.setTitle(new PlainTextConstruct("TEST"));
             myEntry.setContent(new PlainTextConstruct("TEST"));

             DateTime startTime = DateTime.parseDateTime(StartDateTime);
             DateTime endTime = DateTime.parseDateTime(EndDateTime);
             When eventTimes = new When();
             eventTimes.setStartTime(startTime);
             eventTimes.setEndTime(endTime);
             myEntry.addTime(eventTimes);

             CalendarEventEntry insertedEntry = service.update(postUrl, myEntry);

             URL deleteUrl = new URL(insertedEntry.getEditLink().getHref());
             service.delete(deleteUrl);

It has been chopped and changed so much that I am not sure where I am with it now. Has anyone encountered this problem? If so, how did you overcome it? Has anyone got an example of some code that works as Google only provide one line of code in their explanation.

The only thing you need to do is to have an "handle" on your event, there is different way to get it, i'll show you how i do if you want to delete a specific event in your calendar

    String title = "Event title that i want to delete";

    try{
        URL calendarUrl= new URL(calendar.getLink(Link.Rel.ALTERNATE, Link.Type.ATOM).getHref());
        CalendarEventFeed resultFeed = service.getFeed(calendarUrl, CalendarEventFeed.class);

        for (int i = 0; i < resultFeed.getEntries().size(); i++) {
            CalendarEventEntry entry = resultFeed.getEntries().get(i);
            if(entry.getTitle().getPlainText().equals(title)){
                entry.delete();
                break;
            }
        }
    }
    catch (IOException e) {
        e.printStackTrace();
    } 
    catch (ServiceException e) {
        e.printStackTrace();
    }

So you only need to set the title(or anyone of the setting) of a specific calendar, if you don't know how to get the "handle" on the calendar that you have already create earlier, i can explain you how. In my code, "calendar" is a ClendarEntry.

Can you try

CalendarEventEntry insertedEntry = service.insert(postUrl, myEntry);

instead of

CalendarEventEntry insertedEntry = service.update(postUrl, myEntry);

?

I think the rest of the code that you have is for inserting an Event entry and you cannot call update with it. If you change it to insert, it will insert and delete the entry (if it worked) and I don't see a point in it. If you are trying to retrieve an entry and delete it, there are some examples in the post.

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