我正在使用Google的CalDAV功能,并且在尝试更新日历上的事件时遇到问题。
我的PUT请求收到“ 405-不允许使用方法”响应。
$calendar = 'BEGIN:VCALENDAR
PRODID:-//Google Inc//Google Calendar 70.9054//EN
VERSION:2.0
CALSCALE:GREGORIAN
X-WR-CALNAME:Primary Calendar
X-WR-TIMEZONE:America/New_York
BEGIN:VTIMEZONE
TZID:America/New_York
X-LIC-LOCATION:America/New_York
BEGIN:DAYLIGHT
TZOFFSETFROM:-0500
TZOFFSETTO:-0400
TZNAME:EDT
DTSTART:19700308T020000
RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=2SU
END:DAYLIGHT
BEGIN:STANDARD
TZOFFSETFROM:-0400
TZOFFSETTO:-0500
TZNAME:EST
DTSTART:19701101T020000
RRULE:FREQ=YEARLY;BYMONTH=11;BYDAY=1SU
END:STANDARD
END:VTIMEZONE
BEGIN:VEVENT
DTSTART;VALUE=DATE:20181010
DTEND;VALUE=DATE:20181011
DTSTAMP:20181016T192214Z
ORGANIZER;CN=cest@tave.wtf:mailto:cest@tave.wtf
UID:076e0pqgifk0vli961dojq886p@google.com
ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS- ACTION;CN=kody@tave.com;X-NUM-GUESTS=0:mailto:kody@tave.com
ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ- PARTICIPANT;PARTSTAT=ACCEPTED;CN=cest457@tave.wtf;X-NUM-GUESTS=0:mailto:cest457@tave.wtf
CREATED:20181016T132132Z
DESCRIPTION:this is a description from caldav too
LAST-MODIFIED:20181016T192214Z
LOCATION:
SEQUENCE:1
STATUS:CONFIRMED
SUMMARY:test google event 2, updated in caldav stuff
TRANSP:TRANSPARENT
END:VEVENT
END:VCALENDAR';
$url = 'https://apidata.googleusercontent.com/caldav/v2/' . $GoogleCalendarSync->calendarID . '/events';
$opts = [
'allow_redirects' => true,
'timeout' => 40,
'connect_timeout' => 10,
'headers' => [
'Authorization' => 'Bearer ' . $this->config['token']['access_token'],
'If-None-Match' => '*',
'Content-Type' => 'text/calendar',
],
'body' => $calendar,
];
$Client = new GuzzleHttp\Client();
try {
$Response = $Client->request('PUT', $url, $opts);
} catch (Exception $e) {
App::recoverableError(__METHOD__, 'Failed to update calendar', [
'exception' => $e,
'GoogleCalendarSync' => (array) $GoogleCalendarSync,
]);
return false;
}
return (string) $Response->getBody();
我不确定是否使用了错误的URL,尽管我认为它是正确的。 (如果没有,我在哪里可以找到它?)Google的文档在这里非常有限。
我还验证了访问令牌(通过OAuth流程生成)的正确性,因为我还将其用于在另一个单独的请求中成功检索日历。导致问题的原因仅是“ PUT”请求。
感谢您的时间!
答案 0 :(得分:0)
果然,经过数小时的搜索和调整代码,我在提出这个问题后不到30分钟就找到了答案。
问题在于所使用的URL最后缺少日历对象。
url = 'https://apidata.googleusercontent.com/caldav/v2/' . $GoogleCalendarSync->calendarID . '/events';
vs
url = 'https://apidata.googleusercontent.com/caldav/v2/' . $GoogleCalendarSync->calendarID . '/events/basic.ics';