[英]Invalid json error when trying to query google calendar api with access token from Ruby on Rails application
I am trying to set up my Rails application to connect to the google calendar api. 我正在尝试设置我的Rails应用程序以连接到Google Calendar API。 I am following the Oauth2 documentation here: 我在这里关注Oauth2文档:
http://code.google.com/apis/accounts/docs/OAuth2.html#SS http://code.google.com/apis/accounts/docs/OAuth2.html#SS
and the instructions for creating a single event on a user's private calendar here: 以及在此处在用户的私人日历上创建单个事件的说明:
http://code.google.com/apis/calendar/data/2.0/developers_guide_protocol.html#CreatingSingle http://code.google.com/apis/calendar/data/2.0/developers_guide_protocol.html#CreatingSingle
I have been able to generate the Oauth2 access and refresh tokens using the server side strategy, but whenever I query the api I get the following error: 我已经能够使用服务器端策略生成Oauth2访问和刷新令牌,但是每当查询api时,都会出现以下错误:
{"apiVersion":"2.6","error":{"code":400,"message":"Invalid JSON","errors": [{"domain":"GData","code":"invalidJson","internalReason":"Invalid JSON"}]}} {“ apiVersion”:“ 2.6”,“ error”:{“ code”:400,“ message”:“无效的JSON”,“ errors”:[{“ domain”:“ GData”,“ code”:“ invalidJson” ,“ internalReason”:“无效的JSON”}]}}
(This is the same error given in this thread http://groups.google.com/group/google-calendar-help-dataapi/browse_thread/thread/2171226629b28c3b except I am passing GData-Version: 2 into the http post header, so the apiVersion is different ) (这与该线程http://groups.google.com/group/google-calendar-help-dataapi/browse_thread/thread/2171226629b28c3b中给出的错误相同,只是我将GData-Version:2传递到了http post标头中,所以apiVersion不同)
The json I am using is the example event data given in the protocol document in the second reference link above. 我正在使用的json是协议文档中上面第二个参考链接中给出的示例事件数据。 As far as I can tell this is generally happening after the second time I query the api after getting the 302 redirect. 据我所知,这通常是在第二次在获取302重定向后查询api之后发生的。 Would greatly appreciate it if someone would explain what this error means and how to get around it. 如果有人能解释此错误的含义以及解决方法,将不胜感激。 I'm including the relevent code from my app below: 我在下面的应用程序中包含了相关事件代码:
EventController.rb
event_hash = { :data => {
:title => "Tennis with Beth",
:details => "Meet for a quick lesson.",
:transparency => "opaque",
:status => "confirmed",
:location => "Rolling Lawn Courts",
:when => [{
:start => "2012-04-17T15:00:00.000Z",
:end => "2012-04-17T17:00:00.000Z"
}]
}
}
event_data = event_hash.to_json
auth_string = "Bearer " + google_oauth2_credential.access_token # gets access token for user
header_hash = { 'Content-Type' => 'application/json', 'Authorization' => auth_string, 'GData-Version' => '2' }
response = post_to_https('https://www.google.com/calendar/feeds/default/private/full', event_data, header_hash) # note see ApplicationController paste below to see the post_to_https method
if response.code == '302'
uri_string = response.response['Location']
response = post_to_https(uri_string, event_data, header_hash)
render :text => "302 " + response.body
else
render :text => "not 302 " + response.body)
end
ApplicationController.rb
def post_to_https(url_string, params_hash, header_hash = {})
uri = URI.parse(url_string)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data(params_hash)
unless header_hash.empty?
header_hash.each { |key, value| request[key] = value }
end
return http.request(request)
end
Most of the problems you're discussing here should not occur if you use the latest version of the API and the Ruby client library, as documented here: 如果使用最新版本的API和Ruby客户端库,则此处讨论的大多数问题都不会发生,如此处所述:
http://code.google.com/apis/calendar/v3/using.html#setup http://code.google.com/apis/calendar/v3/using.html#setup
page_token = nil
result = result = client.execute(:api_method => service.events.list,
:parameters => {'calendarId' => 'primary'})
while true
events = result.data.items
events.each do |e|
print e.summary + "\n"
end
if !(page_token = result.data.next_page_token)
break
end
result = result = client.execute(:api_method => service.events.list,
:parameters => {'calendarId' => 'primary', 'pageToken' => page_token})
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.