RoR上的Google Calendar Watch API方法存在问题

时间:2014-09-10 20:53:59

标签: notifications google-calendar-api webhooks

我在使用Google日历推送通知(OR web_hook)时遇到问题 - Ruby on Rails中的calendar.event.watch API方法。我正在使用宝石“google-api-client”。然后在rails控制台中尝试执行:

require 'google/api_client'
client = Google::APIClient.new
client.authorization.access_token = "ya29.ewCcVb888OzntBusvtRcZsvfF7kOFMusnyjncR1-FJVr_oX79SgcOMbb"
data = client.execute api_method: service.events.watch, parameters: { id: "my-unique-id-0001", type: "web_hook", calendarId: "primary", address: "https://mywebsite.com/notifications"}

并收到此错误:

#<Google::APIClient::Schema::Calendar::V3::Channel:0x3ffc15ebc944 DATA:{"error"=>{"errors"=>[{"domain"=>"global", "reason"=>"required", "message"=>"entity.resource"}], "code"=>400, "message"=>"entity.resource"}}>> 

2 个答案:

答案 0 :(得分:4)

因为我在这方面浪费了相当多的时间并且在发现问题后感到非常愚蠢,所以对于其他任何犯同样错误的人来说,这是正确答案。

问题是您是在execute方法的parameters选项中传递监视数据。此选项适用于添加到HTTP请求的 URL和查询参数的参数。

但是,watch方法只需要其中一个参数位于查询URL中:calendarId(以便引用日历资源)。所有其他设置应该作为HTTP 请求正文中的JSON对象发送。

原始问题代码的正确形式是

require 'google/api_client'

client = Google::APIClient.new
client.authorization.access_token = "ya29.ewCcVb888OzntBusvtRcZsvfF7kOFMusnyjncR1-FJVr_oX79SgcOMbb"

data = client.execute api_method: service.events.watch, parameters: {
  calendarId: "primary",
}, body_object: {
  id: "my-unique-id-0001",
  type: "web_hook",
  address: "https://mywebsite.com/notifications"
}

body_object选项将传递的对象序列化为JSON,并将其作为HTTP请求体发送,这是watch方法所期望的。

答案 1 :(得分:0)

当您发送请求的正文时,请确保它在JSON中。

所以,如果你有一个哈希,那么json-ify吧。

body_of_request = { id: "my_unique_id", type: web_hook, address: your_address }
body_of_request.to_json

还要确保将请求标头中的内容类型设置为&#39; application / json&#39;。