0

I am trying to find all the freebusy times from my primary calendar using the "query_freebusy" method but I am receiving a "not found" error as a response

I admit I am a novice coder at best, but so far I've been using much of the code from the quickstart-example in the google calendar API documentation to authorize my requests, as well as code from another stack overflow page regarding the query_freebusy method. My code is as follows:

require 'google/apis/calendar_v3'
require 'googleauth'
require 'googleauth/stores/file_token_store'
require 'date'
require 'fileutils'
require 'pry'
require 'pp'

OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'.freeze
APPLICATION_NAME = 'Google Calendar API Ruby Quickstart'.freeze
CREDENTIALS_PATH = 'credentials.json'.freeze
TOKEN_PATH = 'token.yaml'.freeze
SCOPE = Google::Apis::CalendarV3::AUTH_CALENDAR

def authorize
 client_id = Google::Auth::ClientId.from_file(CREDENTIALS_PATH)
 token_store = Google::Auth::Stores::FileTokenStore.new(file: TOKEN_PATH)
 authorizer = Google::Auth::UserAuthorizer.new(client_id, SCOPE, token_store)
 user_id = 'default'
 credentials = authorizer.get_credentials(user_id)
 if credentials.nil?

   url = authorizer.get_authorization_url(base_url: OOB_URI)
   puts 'Open the following URL in the browser and enter the ' \
        "resulting code after authorization:\n" + url
   code = gets
   credentials = authorizer.get_and_store_credentials_from_code(
     user_id: user_id, code: code, base_url: OOB_URI
   )
 end
 credentials
end

service = Google::Apis::CalendarV3::CalendarService.new
service.client_options.application_name = APPLICATION_NAME
service.authorization = authorize

calendar_id = 'primary'
body = Google::Apis::CalendarV3::FreeBusyRequest.new 
body.items = [calendar_id]
body.time_min = "2019-04-28T00:00:00-00:00"
body.time_max = "2019-04-30T00:00:00-00:00"
response = service.query_freebusy(body)
puts response.to_json

The response I get is:

{"calendars":{"":{"busy":[],"errors":[{"domain":"global","reason":"notFound"}]}},"kind":"calendar#freeBusy","timeMax":"2019-04-30T00:00:00.000+00:00","timeMin":"2019-04-28T00:00:00.000+00:00"}

I have 5 events on that calendar between that time frame, but none show up in the response.

Travis
  • 1
  • 1
  • Welcome Travis. Do you have all the code from the example, or just the snippet you posted? – Rots Apr 29 '19 at 01:14
  • I could add all of my code as well – Travis Apr 29 '19 at 02:17
  • Great. Also could you add the error that is printed to the console – Rots Apr 29 '19 at 02:23
  • I changed my code to where I made the request body correctly, so no more 'time min' parameter error anymore but the response gave an error – Travis Apr 29 '19 at 04:21
  • I noticed the scope is different to the example `SCOPE = Google::Apis::CalendarV3::AUTH_CALENDAR` – Rots Apr 29 '19 at 10:36
  • 1
    That was done on purpose to change the access to read and edit, instead of just read. Still, I tried it with the old scope though and it makes no difference. – Travis May 06 '19 at 03:09

0 Answers0