Search code examples
ruby-on-railsgoogle-calendar-apigoogle-api-clientgoogle-api-ruby-client

Locally testing Google API Client watch events


I'm using the google api ruby client to add a webhook to a calendar but I'm running into a Google::Apis::ClientError: pushWebhookBadDomain.

I've set up an ngrok instance and verified it on webmaster tools so that I could add it as an allowed domain to the project that's handling all the calendar auth, etc. I can't find any reference to that specific error so I'm wondering if there's something funky with using ngrok for that and if there are any better ways to go about testing the webhooks locally.

Here's the call (condensed for clarity) in case I'm doing anything stupid:

require 'google/apis/calendar_v3'
client = Google::Apis::CalendarV3::CalendarService.new
authorization = Signet::OAuth2::Client.new(
  :authorization_uri =>
    'https://accounts.google.com/o/oauth2/auth',
  :token_credential_uri =>
    'https://accounts.google.com/o/oauth2/token'
)
authorization.client_id = ENV['GOOGLE_CLIENT_ID']
authorization.client_secret = ENV['GOOGLE_CLIENT_SECRET']
authorization.grant_type = 'refresh_token'
authorization.refresh_token = refresh_token
authorization.fetch_access_token!
client.authorization = authorization

channel = Google::Apis::CalendarV3::Channel.new(address: success_callback_url, id: channel_id, type: "web_hook")
binding.pry
webhook = client.watch_event('primary', channel, single_events: true, time_min: Time.now.iso8601)

Solution

  • When you registered the domain as an Allowed Domain in the Google Cloud Console for your AppEngine project, you should not include https:// in front of your domain name. You need to register it as xxxxxxx.ngrok.io.

    I had the same problem as you and after removing the https:// when adding an Allowed Domain, I'm able to receive Push Notifications on my local machine forwarded using ngrok.

    Let me know if that helps you.

    Br, Ethan