【发布时间】:2015-12-31 05:51:04
【问题描述】:
我有一个 Rails 应用程序,我正在尝试从谷歌获取日历空闲事件。当我运行以下代码时,result = client.execute(.... 方法出现 undefined method "bytesize" for #<Hash.. 错误。我检查了其他一些 stackoverflow 答案,但我看不出我做错了什么。谁能告诉我应该如何处理这个问题?
控制器
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
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
完全错误:
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'
【问题讨论】:
-
你也可以发布完整的错误跟踪吗?
-
qubit,我编辑了代码。响应实际上更长,但我想这就是你需要的。
-
Pardeep,我读过这篇文章,但无法理解。你能告诉我在我的情况下应该如何应用它吗?
标签: ruby-on-rails json api