Undeleting Google Calendar Event

2019-07-20 23:06发布

问题:

I have a c# app which creates events in google calendar. For its own purposes, it controls the event's UID, and sometimes wants to delete and recreate events.

When trying to recreate an event with the same UID as a deleted event, it gets "The remote server returned an error: (409) Conflict."

I can see the deleted events by apending "?showdeleted=true&showhidden=true" to a request URL.

However I do not see anything in the API which allows me to undelete deleted events after retrieving them.

Any suggestions?

UPDATE:

Trying Jay's suggesting I have something like

var service = new CalendarService();
service.setUserCredentials("XXX", "XXX");
var query = new CalendarQuery { Uri = new Uri("http://www.google.com/calendar/feeds/default/owncalendars/full") };
var cal = service.Query(query).Entries
    .Select (e => new { Title = e.Title.Text, Uri = e.SelfUri, 
        Id = e.SelfUri.Content.Split('/').Last () } )
    .Single (e => e.Title == calendarName);
var eventQuery = new EventQuery(string.Format(@"http://www.google.com/calendar/feeds/{0}/private/full?showdeleted=true&showhidden=true", cal.Id));
var evs = service.Query(eventQuery).Entries.Cast<EventEntry>().ToList();
evs[0].Status = EventEntry.EventStatus.CONFIRMED;
service.Update(ev[0]);

And it is giving me "The remote server returned an error: (404) Not Found."

回答1:

It appears that the

The remote server returned an error: (404) Not Found

Is due to the EventEntry.EditUri not being able to find the deleted / hidden events.

This can be fixed by appending "?showdeleted=true&showhidden=true" to the EditUri, before the EntryEvent.Update.

Something along the lines of:

var evs = service.Query(eventQuery).Entries.Cast<EventEntry>().ToList();
evs[0].Status = EventEntry.EventStatus.CONFIRMED;
evs[0].EditUri.Content = evs[0].EditUri.Content + "?showdeleted=true&showhidden=true";
evs[0].Update();


回答2:

You can undelete an event by changing it's status attribute from cancelled to confirmed

You can try this for yourself with an events.patch() operation in the Google API Explorer. You'll need to turn on OAuth authorization and then input the calendar and event IDs.