Google Calendar API - can only update event once

2019-05-04 07:23发布

问题:

I've encountered the same problem as described in this post:

Google Calendar api v3 re-update issue

Namely, once I create an event and update it once using the Google Calendar API (v3), I am no longer able to update the event. When I attempt to, I get a 400 - Invalid value response. (FWIW I'm working in PHP).

Following a lead offered in the post I referenced above, I attempted to solve the issue using etags (though admittedly my grasp of how they work is limited). Basically, on event update, the API returns an etag in its response, which I now save in my database. Then for subsequent (n > 1) updates, I pull the current etag from the database and include it in the http header:

Content-Type:  application/json
Authorization:  OAuth [token]
If-Match: [etag]

This follows info under the "Updating Entries" header here: http://code.google.com/apis/gdata/docs/2.0/reference.html#ResourceVersioning

Sidenote: in the google ref above, the If-Match header is shown as

If-Match: "S0wCTlpIIip7ImA0X0QI"

with double quotes around the etag. I'm saving the etags in the database with double quotes, exactly as I receive them in first update response. Do I need to escape the quotes or anything when adding to the header using curl_setopt/HTTPHEADER?

Despite implementing this etag If-Match thing, I'm still getting the same 400 - Invalid value response. I know that my request bodies are valid because the first update works fine. There's just some additional issue surrounding subsequent updates.

Any help much appreciated.

回答1:

Make sure to increment the sequence number field when updating events.



回答2:

I had same problem. To increment the sequence number you need to keep track of how many updates you have done and include the next increment in your update. For some reason the first update doesn't require this but subsequent updates do require it.

Using the google php library on your 2nd update to the event would look something like this (minus whatever else your are updating):

$calendarID = ID_OF_YOUR_CALENDAR;
$eventID = ID_OF_YOUR_EVENT;

$event = new Google_Event();
$event->setSequence('2');

$calEvent = $cal->events->update($calendarID $eventID, $event);


回答3:

That is how I do it. Fetch the entry from Google so it already has the latest Etag set and increment the sequence by one and update the entry.

As I use java so following is a example with java:

com.google.api.services.calendar.model.Event googleCalendarEvent = service.events().get(clientCalendarEvent.getCalendar().getCalendarKey(),clientCalendarEvent.getEventKey()).execute();
updateGoogleCalendarEvent(clientCalendarEvent, googleCalendarEvent);

googleCalendarEvent.setSequence(googleCalendarEvent.getSequence() + 1);

com.google.api.services.calendar.model.Event event = service
      .events()
      .update(clientCalendarEvent.getCalendar().getCalendarKey(), clientCalendarEvent.getEventKey(),
          googleCalendarEvent).execute();