I’m attempting to integrate my webapp’s calendar with Google Calendar. I’m successfully authenticating the user, able to read events from Google and delete events from Google. I’m having trouble updating events from my app to Google.
I’m using the PATCH method described here as I only want to modify the fields that I send in the API call (as opposed to the PUT call which requires all fields for the event).
My Perl code create a JSON object representing what I want to change. I then submit a PATCH request to Google, and I receive status 200 OK and the event resource back from Google. What I receive back is supposed to be the modified event resource, but the fields I have modified are not modified in the response. When I check in Google Calendar itself, once again I see the event is not modified.
I’m confused as to why I’m getting the 200 OK response instead of an error, when the event is not updating. Curiously, in the response, the “updated” timestamp property has been updated, but the summary field has not.
my $ua = LWP::UserAgent->new;
my $url = "https://www.googleapis.com/calendar/v3/calendars/primary/events/${id}?access_token=${access_token}";
my $resource{"summary"} = "$g{summary}";
$resource = encode_json(\%resource);
my $headers = HTTP::Headers->new();
$headers->header(If_Match => "$etag");
my $request = HTTP::Request->new('PATCH',$url,$headers,$resource);
my $response = $ua->request($request);
my $status = $response->status_line;
my $result = decode_json($response->decoded_content);
The “etag” value from the result is updated as I expect, which indicates that the calendar event is being (somewhat) modified in Google.
Some Data::Dumper tracing:
The request:
$VAR1 = bless( {
'_content' => '{"summary":"End of season function MODIFIED"}',
'_uri' => bless( do{\(my $o = 'https://www.googleapis.com/calendar/v3/calendars/primary/events/<eventID>?access_token=<access_token>')}, 'URI::https' ),
'_headers' => bless( {
'if-match' => '<etag>'
}, 'HTTP::Headers' ),
'_method' => 'PATCH'
}, 'HTTP::Request' );
The response:
$VAR1 = bless( {
'_protocol' => 'HTTP/1.1',
'_content' => '{
"kind": "calendar#event",
"etag": "<etag>",
"id": "<eventID>",
"status": "confirmed",
"htmlLink": "<suppressed>",
"created": "2012-03-08T05:06:04.000Z",
"updated": "2012-03-25T09:18:49.000Z",
"summary": "End of season function",
"creator": {
"email": "<suppressed>"
},
"organizer": {
"email": "<suppressed>"
},
"start": {
"date": "2012-03-24"
},
"end": {
"date": "2012-03-24"
},
"iCalUID": "<suppressed>",
"sequence": 1,
"reminders": {
"useDefault": true
}
}
',
'_rc' => '200',
'_headers' => bless( {
'connection' => 'close',
'cache-control' => 'no-cache, no-store, max-age=0, must-revalidate',
'date' => 'Sun, 25 Mar 2012 09:18:49 GMT',
'client-ssl-cert-issuer' => '/C=US/O=Google Inc/CN=Google Internet Authority',
'client-ssl-cipher' => 'ECDHE-RSA-RC4-SHA',
'client-peer' => '173.194.72.95:443',
'client-date' => 'Sun, 25 Mar 2012 09:18:49 GMT',
'pragma' => 'no-cache',
'content-type' => 'application/json; charset=UTF-8',
'x-xss-protection' => '1; mode=block',
'server' => 'GSE',
'client-ssl-socket-class' => 'IO::Socket::SSL',
'client-response-num' => 1,
'etag' => '<etag>',
'x-frame-options' => 'SAMEORIGIN',
'x-content-type-options' => 'nosniff',
'client-ssl-cert-subject' => '/C=US/ST=California/L=Mountain View/O=Google Inc/CN=*.googleapis.com',
'expires' => 'Fri, 01 Jan 1990 00:00:00 GMT'
}, 'HTTP::Headers' ),
'_msg' => 'OK',
'_request' => bless( {
'_content' => '{"summary":"End of season function MODIFIED"}',
'_uri' => bless( do{\(my $o = 'https://www.googleapis.com/calendar/v3/calendars/primary/events/<eventID>?access_token=<access_token>')}, 'URI::https' ),
'_headers' => bless( {
'user-agent' => 'libwww-perl/6.01',
'if-match' => '<etag>'
}, 'HTTP::Headers' ),
'_method' => 'PATCH',
'_uri_canonical' => $VAR1->{'_request'}{'_uri'}
}, 'HTTP::Request' )
}, 'HTTP::Response' );
Can anyone see what I’m doing wrong? I’m sure that Google is accepting my PATCH request, but for some reason not exactly seeing the field I want to modify. I’ve tried submitting the exact same JSON object on their testing page and have no problem getting that to work…
I found the solution purely by trial and error – adding the “Content-Type” header to the HTTP header seemed to do the trick: