我尝试使用Google's API library for Ruby通过CalDAV API访问Google日历(我使用服务帐户作为应用程序类型)。
我已经编写了以下代码,但是当HTTP方法是PUT或POST并且输出错误时,这不起作用"不允许使用HTTP方法"状态代码为405.但是,当方法为GET或DELETE时,它可以正常工作。
require 'google/api_client'
client = Google::APIClient.new(
application_name: 'test',
application_version: '1.0.0'
)
calendar_id = 'CALENDER_ID'
BASE_URL = "https://apidata.googleusercontent.com/caldav/v2/#{calendar_id}/events/"
key = Google::APIClient::KeyUtils.load_from_pkcs12('P12_FILE', 'notasecret')
client.authorization = Signet::OAuth2::Client.new(
token_credential_uri: 'https://www.googleapis.com/oauth2/v3/token',
audience: 'https://www.googleapis.com/oauth2/v3/token',
scope: 'https://www.googleapis.com/auth/calendar',
issuer: 'foobar@developer.gserviceaccount.com',
signing_key: key)
client.authorization.fetch_access_token!
body = <<EOS
BEGIN:VCALENDAR
VERSION:2.0
PRODID:test
CALSCALE:GREGORIAN
METHOD:PUBLISH
X-WR-CALNAME:CAMPHOR- Schedule 201503
BEGIN:VTIMEZONE
TZID:Asia/Tokyo
BEGIN:STANDARD
DTSTART:19700101T000000
TZOFFSETFROM:+0900
TZOFFSETTO:+0900
TZNAME:JST
END:STANDARD
END:VTIMEZONE
BEGIN:VEVENT
DTSTAMP:20150224T080050Z
UID:4807869c-ba02-48cc-aed5-0e0f5ff19022
DTSTART:20150331T150000
DTEND:20150331T200000
DESCRIPTION:
SUMMARY:DEST 2015-03-31 15:00:00 +0900 2015-03-31 20:00:00 +0900
END:VEVENT
END:VCALENDAR
EOS
request = Google::APIClient::Request.new(
uri: BASE_URL,
body: body,
http_method: :post,
headers: {
'Content-Type' => 'application/xml; charset=utf-8'
}
)
result = client.execute(request)
puts result.response.status
puts result.response.env
您能否告诉我如何修复此代码以便能够使用所有方法?
答案 0 :(得分:1)
最初的问题是你不能像CalDAV那样PUT到集合资源。有关详细信息,请参阅规范的section 5.3.2。换句话说,URL应该类似于“... / events / somerandomeventid.ics”
那就是说,你在这里走的是一条糟糕的道路。