Issues with the Google Calendar Watch API on RoR

I have a problem with Google Push Push Notifications (OR web_hook) - calendar.event.watch API method in Ruby on Rails. I am using the gem "google-api-client". Then in the console rails tries 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 get this error:

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

      

+3


source to share


2 answers


Because I spent quite a bit of time on it and felt pretty stupid to find out what the problem was, here is the correct answer to that question in case someone else makes the same mistake.

The problem is you are passing clock data in the options of parameters

the execute method. This parameter is for parameters that are added to the URL and request parameters of the HTTP request.

However, the watch method only expects one of these parameters to be in the request url: calendarId

(for a reference to a calendar resource). All other settings must be sent as a JSON object in an HTTP tag...



So the correct form of the question's source code is

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 parameter body_object

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

+3


source


When you submit the request body, make sure it is in JSON.

So, if you have a hash then json-ify is.



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

      

Also don't forget to set the content type in the request header to 'application / json'.

0


source







All Articles