Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"}}>> 
like image 810
Станислав Герняк Avatar asked May 13 '26 21:05

Станислав Герняк


1 Answers

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.

like image 78
rtytgat Avatar answered May 16 '26 14:05

rtytgat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!