Google Calendar API - PATCH返回200 OK但实际上没有更新事件

时间:2012-03-25 09:28:56

标签: perl google-api google-calendar-api

我正在尝试将我的webapp日历与Google日历集成。我已成功验证用户身份,能够从Google读取事件并从Google中删除事件。我无法将应用中的活动更新为Google。

我使用所描述的方法PATCH here如我仅要修改我在API调用(而不是在PUT调用这需要针对事件的所有字段)发送的字段。

My Perl代码创建一个JSON对象,表示我想要更改的内容。然后,我向Google提交了一个PATCH请求,我收到状态200 OK,并从Google返回事件资源。我收到的内容应该是修改后的事件资源,但我修改的字段不会在响应中修改。当我签入Google日历本身时,我再次看到该事件未被修改。

当我没有更新时,我很困惑为什么我得到200 OK响应而不是错误。奇怪的是,在响应中,“已更新”的时间戳属性已更新,但摘要字段尚未更新。

  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);

结果中的“etag”值会按照我的预期更新,这表示Google中正在(稍微)修改日历事件。

一些Data :: Dumper跟踪:

请求:

$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' );

回应:

$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' );

谁能看到我做错了什么?我确信Google正在接受我的PATCH请求,但由于某种原因,我没有完全看到我要修改的字段。我已经尝试在testing page上提交完全相同的JSON对象,并且没有问题让它工作......

2 个答案:

答案 0 :(得分:3)

我完全通过试验和错误找到了解决方案 - 将“Content-Type”标头添加到HTTP标头似乎可以解决问题:

  my $headers = HTTP::Headers->new();
  $headers->header(If_Match => "$etag");
  $headers->header(Content_Type => "application/json");

答案 1 :(得分:0)

添加标头accept = application / json为我修复了