繁体   English   中英

尝试使用Ruby on Rails应用程序中的访问令牌查询Google Calendar API时出现无效的json错误

[英]Invalid json error when trying to query google calendar api with access token from Ruby on Rails application

我正在尝试设置我的Rails应用程序以连接到Google Calendar API。 我在这里关注Oauth2文档:

http://code.google.com/apis/accounts/docs/OAuth2.html#SS

以及在此处在用户的私人日历上创建单个事件的说明:

http://code.google.com/apis/calendar/data/2.0/developers_guide_protocol.html#CreatingSingle

我已经能够使用服务器端策略生成Oauth2访问和刷新令牌,但是每当查询api时,都会出现以下错误:

{“ apiVersion”:“ 2.6”,“ error”:{“ code”:400,“ message”:“无效的JSON”,“ errors”:[{“ domain”:“ GData”,“ code”:“ invalidJson” ,“ internalReason”:“无效的JSON”}]}}

(这与该线程http://groups.google.com/group/google-calendar-help-dataapi/browse_thread/thread/2171226629b28c3b中给出的错误相同,只是我将GData-Version:2传递到了http post标头中,所以apiVersion不同)

我正在使用的json是协议文档中上面第二个参考链接中给出的示例事件数据。 据我所知,这通常是在第二次在获取302重定向后查询api之后发生的。 如果有人能解释此错误的含义以及解决方法,将不胜感激。 我在下面的应用程序中包含了相关事件代码:

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

如果使用最新版本的API和Ruby客户端库,则此处讨论的大多数问题都不会发生,如此处所述:

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.

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