James Sherriff
James Sherriff

Reputation: 15

How to use google-api-client-ruby calendar stop_channel

I am attempting to use the google-api-client-ruby gem to interface with Google calendar.

I have succesfully setup a channel to watch for events, but I'm struggling to use the stop_channel method to stop watching for events.

The source code looks like it takes a channel object, but if I create a channel object and pass it to the stop_channel method, I get:

Error - Google::Apis::ClientError: required: Required

My code..

channel = Google::Apis::CalendarV3::Channel.new(address: "https://www.google.com/", id: 1, type: "web_hook")
begin
  calendar_service.stop_channel(channel)
rescue => error
  puts error
end

Am I doing something wrong or is the gem not working?..

Upvotes: 0

Views: 814

Answers (2)

Doug Couvillion
Doug Couvillion

Reputation: 1161

This question is pretty old at this point but I faced the exact same issue with the newer google-apis-calendar_v3 gem so I thought I'd post my answer.

In order to get my code working I had to create the channel object with the id and resource_id first. You might assume just the id would be sufficient but apparently it is not.

Here's the snippet of code that worked for me.

# Handle Google OAuth
# I'm not sure you need both of these scopes to stop watching a channel but
# my application needed both for other things it had to handle.
client_id = Google::Auth::ClientId.from_file("#{ROOT_DIR}/client_secret.json")
scopes = [Google::Apis::CalendarV3::AUTH_CALENDAR_EVENTS,
          Google::Apis::CalendarV3::AUTH_CALENDAR_READONLY]
token_store = Google::Auth::Stores::RedisTokenStore.new(redis: Redis.new)
authorizer = Google::Auth::WebUserAuthorizer.new(client_id,
                                                 scopes,
                                                 token_store,
                                                 '/oauth2callback')

# Create the CalendarService object
service = Google::Apis::CalendarV3::CalendarService.new
service.client_options.application_name = "Quickstart"

if service.authorization.nil?
  service.authorization = authorizer.get_credentials(user_id, request)
end

# Instantiate the channel we want to stop watching using the channel id and
# resource id passed in as request parameters.
channel = Google::Apis::CalendarV3::Channel.new(id: params['cid'],
                                                type: 'web_hook',
                                                resource_id: params['resource_id'])

# Stop watching the channel.
service.stop_channel(channel)

Hopefully this will be useful to others trying to integrate Google Calendar with a Ruby application.

Upvotes: 0

abielita
abielita

Reputation: 13494

Your error Google::Apis::ClientError: required: Required means that the request is invalid and should not be retried without modification.

Also, I have seen this sample code that uses stop_channel method.

def stop_channel(channel_id, resource_id)
    channel = Google::Apis::CalendarV3::Channel.new(
      id: channel_id,
      resource_id: resource_id
    )
    service.stop_channel(channel)
end

Upvotes: 2

Related Questions