rails第三方API响应没有bitesize错误

时间:2015-12-31 05:51:05

标签: ruby-on-rails json api

我有一个rails应用程序,我正试图从谷歌获取日历freebusy事件。当我运行以下代码时,result = client.execute(....方法出现 include GoogleCalendarApi ...... @user = current_user @google = @user.socials.where(provider: "google_oauth2").first unless @google.blank? # @client = get_busy_events(@google) # @result = open_gcal_connection(get_busy_events, @client, @google) @result = get_busy_events(@google) end ..... 错误。我检查了一些其他stackoverflow的答案,但我看不出我做错了什么。任何人都可以告诉我应该如何处理这个问题?

控制器

def init_google_api_calendar_client(google_account)
  #method only called if google_oauth2 social exists
  client = Google::APIClient.new
  client.authorization.access_token = google_account.token
  client.authorization.client_id = ENV['GOOGLE_API_KEY']
  client.authorization.client_secret = ENV['GOOGLE_API_SECRET']
  client.authorization.refresh_token = google_account.refresh_token
  return client
end

def get_busy_events(social_object)
  client = init_google_api_calendar_client(social_object)
  old_token = client.authorization.access_token
  service = client.discovered_api('calendar', 'v3')

  result = client.execute(
    api_method: service.freebusy.query,
    body: { timeMin: '2015-12-24T17:06:02.000Z',
            timeMax: '2016-01-30T17:06:02.000Z',
            items: [{ id: social_object.email }]},
   headers: {'Content-Type' => 'application/json'})

   new_token = client.authorization.access_token
   if old_token != new_token
    social_object.update_attribute(token: new_token)
   end

  return result
end

LIB / google_calendar_api.rb

Google::APIClient - Initializing client with options {}
21:33:15 puma.1       | Google::APIClient - Please provide :application_name and :application_version when initializing the client
21:33:15 puma.1       | Google::APIClient::Request Sending API request get https://www.googleapis.com/discovery/v1/apis/calendar/v3/rest {"User-Agent"=>"google-api-ruby-client/0.8.6 Mac OS X/10.10.4\n (gzip)", "Accept-Encoding"=>"gzip", "Content-Type"=>""}
21:33:15 puma.1       | Decompressing gzip encoded response (12528 bytes)
21:33:15 puma.1       | Decompressed (103479 bytes)
21:33:15 puma.1       | Google::APIClient::Request Result: 200 {"expires"=>"Thu, 31 Dec 2015 05:36:09 GMT", "date"=>"Thu, 31 Dec 2015 05:31:09 GMT", "etag"=>"\"ye6orv2F-1npMW3u9suM3a7C5Bo/U5WRLEvUgzkUohB7qwzTs2ir15o\"", "vary"=>"Origin, X-Origin", "content-type"=>"application/json; charset=UTF-8", "x-content-type-options"=>"nosniff", "x-frame-options"=>"SAMEORIGIN", "x-xss-protection"=>"1; mode=block", "content-length"=>"12528", "server"=>"GSE", "age"=>"126", "cache-control"=>"public, max-age=300, must-revalidate, no-transform", "alternate-protocol"=>"443:quic,p=1", "alt-svc"=>"quic=\":443\"; ma=604800; v=\"30,29,28,27,26,25\"", "connection"=>"close"}
21:33:15 puma.1       | Google::APIClient::Request Sending API request post https://www.googleapis.com/calendar/v3/freeBusy {"User-Agent"=>"google-api-ruby-client/0.8.6 Mac OS X/10.10.4\n (gzip)", "Content-Type"=>"application/json", "Accept-Encoding"=>"gzip", "Authorization"=>"Bearer ya29.WQKv43gUEb0Jt3jTBevBs0_Z9VurfGxmbH8Knv8E9Sqbw4zxeCHjydwUeyo3MSAotYj0", "Cache-Control"=>"no-store"}
21:33:15 puma.1       | Completed 500 Internal Server Error in 382ms (ActiveRecord: 0.8ms)
21:33:15 puma.1       | 
21:33:15 puma.1       | NoMethodError - undefined method `bytesize' for #<Hash:0x007fb6d25b9330>:
21:33:15 puma.1       |   /Users/Silo/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/net/http/generic_request.rb:182:in `send_request_with_body'
21:33:15 puma.1       |   /Users/Silo/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/net/http/generic_request.rb:120:in `exec'

完整错误:

recaptcha

1 个答案:

答案 0 :(得分:1)

从Pardeep的链接中,尝试

result = client.execute(
    api_method: service.freebusy.query,
    body: URI.encode_www_form({ timeMin: '2015-12-24T17:06:02.000Z',
            timeMax: '2016-01-30T17:06:02.000Z',
            items: [{ id: social_object.email }]}),
   headers: {'Content-Type' => 'application/json'})

问题是身体无法作为哈希发送。你应该编码为一个字符串。