Googleカレンダー apiに接続するようにRailsアプリケーションを設定しようとしています。私はここのOAuth2のドキュメントを、次の午前:私はのOAuth2を生成することができたRuby on Railsアプリケーションのアクセストークンを使用してGoogleカレンダーのAPIを照会しようとしたときにjsonエラーが発生しました
http://code.google.com/apis/calendar/data/2.0/developers_guide_protocol.html#CreatingSingle
:
http://code.google.com/apis/accounts/docs/OAuth2.html#SS
、ここでユーザーのプライベート カレンダー上の単一のイベントを作成するための手順アクセスとリフレッシュのトークン サーバー側の戦略を使用して、私はAPIをクエリするたびに私は の後に を得るコード ":400、"メッセージ ":"無効 " JSON"、 "errors": [{"domain": "GData"、 "code"> "apiVersion": "2.6"、 "error" "invalidJson"、 "internalReason": "無効 JSON"}]}}
(これは、このスレッドに与えられた同じエラーである http://groups.google.com/group/google-calendar-help-dataapi/browse_thread/thread/2171226629b28c3b IはGDataの-版通過い除い:HTTPのPOSTヘッダに2を、したがって apiVersionは異なります)
私が使用しているjsonは、上記の2番目のリファレンスリンクの ドキュメントに記載されているイベントデータ例です。限り、私はこれを言うことができる は、一般的に私は30235リダイレクトを取得した後に、 の後にAPIを照会する2回目の後に起きている。誰かが にこのエラーの意味とその回避方法を説明すると大いに感謝します。あなたはAPIの最新バージョンとRubyクライアントライブラリを使用している場合は、ここで説明されているようあなたがここで議論している問題のほとんどは、発生しません
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