简体   繁体   English

Rails没有bitizeize错误到第三方API响应

[英]rails no bitesize error to 3rd party API response

I have a rails app and I'm trying to get calendar freebusy events from google. 我有一个Rails应用,我正在尝试从Google获取日历忙碌事件。 When I run the following code I get undefined method "bytesize" for #<Hash.. error for the result = client.execute(.... method. I checked out some other stackoverflow answers, but I can't see what I'm doing wrong. Anyone can tell me how I should deal with this problem? 当我运行以下代码时,我得到undefined method "bytesize" for #<Hash..错误的undefined method "bytesize" for #<Hash.. result = client.execute(....方法。我检查了其他一些stackoverflow答案,但是我看不到“做错了。任何人都可以告诉我该如何处理这个问题?

controller 控制者

  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
    .....

lib/google_calendar_api.rb lib / google_calendar_api.rb

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

full error: 完全错误:

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'

From Pardeep's link, try 在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'})

The problem is that the body could not be sent as a hash. 问题是正文不能作为哈希发送。 You should encode as an string. 您应该编码为字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM