Problems with Google Calendar Watch API method on RoR

I have a problem with Google Calendar Push Notifications (OR web_hook) - calendar.event.watch API method in Ruby on Rails. I am using a gem "google-api-client". Then in rails console trying to do:

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"}

and getting this error:

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

Upvotes: 3

Views: 1847

Answers (2)

rtytgat
rtytgat

Reputation: 497

Because I wasted quite some time on this and felt quite stupid after finding out what the problem was, here is the correct answer to this question for anyone else making the same mistake.

The problem is that you are passing the watch data in the parameters option of the execute method. This option is meant for parameters that are added to the URL and query parameters of the HTTP request.

The watch method, however, only expects one of these parameters to be in the query URL: the calendarId (in order to reference the calendar resource). All other settings are supposed to be sent as a JSON object in the HTTP request body.

The correct form of the original question's code is therefore

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"
}

The body_object option serializes the passed object to JSON and sends it as the HTTP request body, which is what the watch method expects.

Upvotes: 4

dannycodes
dannycodes

Reputation: 17

When you send in the body of the request, make sure it's in JSON.

So if you have a hash then json-ify it.

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

Also make sure to set the content-type in the request header to 'application/json'.

Upvotes: 0

Related Questions