我正在尝试使用 Ruby gem RestClient 为我的一个 Fusion Tables 更新样式。
这是我的代码:
require 'rest_client'
tableId = '<STRING CONTAINING TABLEID>'
styleId = '<STRING CONTAINING STYLEID>'
key = '<STRING CONTAINING MY FUSION TABLES API KEY>'
table_url = "https://www.googleapis.com/fusiontables/v1/tables/#{tableId}/styles/#{styleId}?key=#{key}"
update = '{"polygonOptions": {"strokeColor":"#ffffff"}}'
token = 'STRING CONTAINING AUTHORIZATION TOKEN'
RestClient.put table_url,update,{"Authorization" => "Bearer #{token}"}
当我运行该代码时,出现此错误:
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/abstract_response.rb:48:in `return!': 400 Bad Request (RestClient::BadRequest)
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/request.rb:230:in `process_result'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/request.rb:178:in `block in transmit'
from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:745:in `start'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/request.rb:172:in `transmit'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/request.rb:64:in `execute'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/request.rb:33:in `execute'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient.rb:80:in `put'
当我将更新
代码输入Google's official Style request PUT maker thingie时,更新有效。但是当我运行我的 Ruby 代码时它不起作用。
有人知道我做错了什么吗?
编辑:我通过添加 RestClient.log = logger
RestClient.put "https://www.googleapis.com/fusiontables/v1/tables/<MY TABLE ID HERE>/styles/4?key=<AND HERE'S WHERE MY FUSION TABLE API KEY GOES>", "{\"polygonOptions\":{\"strokeColor\":\"#ffffff\"}}", "Accept"=>"*/*; q=0.5, application/xml", "Accept-Encoding"=>"gzip, deflate", "Authorization"=>"Bearer <THIS CONTAINS THE BEARER STRING>", "Content-Length"=>"44"
# => 400 BadRequest | application/json 255 bytes
最佳答案
你真的应该使用 google-api-ruby-client库,而不是构建您自己的 REST 调用。该库为您提取了大量 OAuth 内容和参数格式。
话虽如此,你能不能enable debugging for your RestClient并发布 RestClient 调用的输出以及 Google 官方 PUT 制造商 thingie 的输出(我喜欢你的技术术语)?比较两者应该会显示它们的不同之处以及 Google 不喜欢您的内容的地方。
关于ruby - 融合表 : Why do I keep getting a "400 Bad Request" error when trying to update a table style via Ruby's RestClient gem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14572711/