I'm trying to update a style for one of my Fusion Tables by using the Ruby gem RestClient.
我正在尝试使用Ruby gem RestClient更新我的一个Fusion Tables的样式。
Here's my code:
这是我的代码:
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}"}
When I run that code, I get this error:
当我运行该代码时,我收到此错误:
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'
When I input the update
code into Google's official Style request PUT maker thingie, the update works. But it does not work when I run my Ruby code.
当我将更新代码输入到Google的官方样式请求PUT制造商的东西时,更新工作。但是当我运行我的Ruby代码时它不起作用。
Does anyone know what I am doing wrong?
有谁知道我做错了什么?
EDIT: Extra output I get from adding in RestClient.log = logger
编辑:我在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
6 个解决方案
#1
4
You really should be using the google-api-ruby-client library instead of building your own REST calls. The library abstracts a lot of the OAuth stuff and formatting of the parameters for you.
你真的应该使用google-api-ruby-client库而不是构建自己的REST调用。该库为您提取了大量OAuth内容和参数格式。
Having said that, can you enable debugging for your RestClient and post the output of the RestClient call along with the output from Google official PUT maker thingie (I like your technical jargon there)? Comparing the two should show how they differ and what Google doesn't like with yours.
话虽如此,您是否可以为您的RestClient启用调试并发布RestClient调用的输出以及Google官方PUT制造商的输出(我喜欢您的技术术语)?比较两者应该显示它们之间的差异以及Google与您的不同之处。
#2
4
I think the problem is that you are not setting the content-type to application/json. Try to do something like this:
我认为问题是你没有将内容类型设置为application / json。尝试做这样的事情:
RestClient.put(table_url, update, {"Authorization" => "Bearer #{token}", "Content-type" => "application/json"})
The payload in this case needs to be json so you can either use your json-string from your example or run to_json on you data structure.
在这种情况下,有效负载需要是json,因此您可以使用示例中的json-string或在数据结构上运行to_json。
#3
4
In answer to your question about setting up a logger without Rails, in the comments to @Jay Lee's answer…
在回答你关于在没有Rails的情况下设置记录器的问题时,在评论@Jay Lee的回答中......
Here is a logger set up to output to standard out:
这是一个设置为输出到标准输出的记录器:
logger = Logger.new STDOUT
logger.level = Logger::WARN # INFO/DEBUG… whatever level you find is needed
logger.datetime_format = '%a %d-%m-%Y %H%M '
Then put the rest of your code into a console (e.g. IRB) and before the last line add:
然后将其余代码放入控制台(例如IRB)并在最后一行添加之前:
RestClient.log = logger
and you should get some helpful information output to the terminal. See the documents for the Logger class for more information on the levels available.
你应该得到一些有用的信息输出到终端。有关可用级别的更多信息,请参阅Logger类的文档。
#4
2
It could be because your hash here
可能是因为你的哈希在这里
update = '{"polygonOptions": {"strokeColor":"#ffffff"}}'
should probably be
应该是
update = {"polygonOptions" => {"strokeColor" => "#ffffff"}}
Good luck!
#5
1
FYI,
Alternatives to try:
尝试的替代方案:
1) Remove HTTPS request in project settings and token regeneration.
1)删除项目设置和令牌重新生成中的HTTPS请求。
2) Try to use SSL in that case.
2)在这种情况下尝试使用SSL。
3) Sometimes this error occurs when the values exceeds more than 255 characters that may be the possibility here with you. The same problem occurred sometime back with someone and was resolved after debugging. Check the link for more information.
3)有时,当值超过255个字符时,会出现此错误,这可能与您在一起。某个时候发生了同样的问题,并在调试后得到了解决。查看链接以获取更多信息。
#6
0
I was having the same 400 Bad Request problem, specifically when posting styles. I was able to solve the problem by making sure any values for "kind" in the style were namespaced, which the examples in the docs don't always get right -- for example:
我有相同的400 Bad Request问题,特别是在发布样式时。我能够通过确保样式中“kind”的任何值都是命名空间来解决问题,而文档中的示例并不总是正确的 - 例如:
{
...
"kind": "fusiontables#buckets"
...
}
Instead of just "buckets".
而不仅仅是“水桶”。
#1
4
You really should be using the google-api-ruby-client library instead of building your own REST calls. The library abstracts a lot of the OAuth stuff and formatting of the parameters for you.
你真的应该使用google-api-ruby-client库而不是构建自己的REST调用。该库为您提取了大量OAuth内容和参数格式。
Having said that, can you enable debugging for your RestClient and post the output of the RestClient call along with the output from Google official PUT maker thingie (I like your technical jargon there)? Comparing the two should show how they differ and what Google doesn't like with yours.
话虽如此,您是否可以为您的RestClient启用调试并发布RestClient调用的输出以及Google官方PUT制造商的输出(我喜欢您的技术术语)?比较两者应该显示它们之间的差异以及Google与您的不同之处。
#2
4
I think the problem is that you are not setting the content-type to application/json. Try to do something like this:
我认为问题是你没有将内容类型设置为application / json。尝试做这样的事情:
RestClient.put(table_url, update, {"Authorization" => "Bearer #{token}", "Content-type" => "application/json"})
The payload in this case needs to be json so you can either use your json-string from your example or run to_json on you data structure.
在这种情况下,有效负载需要是json,因此您可以使用示例中的json-string或在数据结构上运行to_json。
#3
4
In answer to your question about setting up a logger without Rails, in the comments to @Jay Lee's answer…
在回答你关于在没有Rails的情况下设置记录器的问题时,在评论@Jay Lee的回答中......
Here is a logger set up to output to standard out:
这是一个设置为输出到标准输出的记录器:
logger = Logger.new STDOUT
logger.level = Logger::WARN # INFO/DEBUG… whatever level you find is needed
logger.datetime_format = '%a %d-%m-%Y %H%M '
Then put the rest of your code into a console (e.g. IRB) and before the last line add:
然后将其余代码放入控制台(例如IRB)并在最后一行添加之前:
RestClient.log = logger
and you should get some helpful information output to the terminal. See the documents for the Logger class for more information on the levels available.
你应该得到一些有用的信息输出到终端。有关可用级别的更多信息,请参阅Logger类的文档。
#4
2
It could be because your hash here
可能是因为你的哈希在这里
update = '{"polygonOptions": {"strokeColor":"#ffffff"}}'
should probably be
应该是
update = {"polygonOptions" => {"strokeColor" => "#ffffff"}}
Good luck!
#5
1
FYI,
Alternatives to try:
尝试的替代方案:
1) Remove HTTPS request in project settings and token regeneration.
1)删除项目设置和令牌重新生成中的HTTPS请求。
2) Try to use SSL in that case.
2)在这种情况下尝试使用SSL。
3) Sometimes this error occurs when the values exceeds more than 255 characters that may be the possibility here with you. The same problem occurred sometime back with someone and was resolved after debugging. Check the link for more information.
3)有时,当值超过255个字符时,会出现此错误,这可能与您在一起。某个时候发生了同样的问题,并在调试后得到了解决。查看链接以获取更多信息。
#6
0
I was having the same 400 Bad Request problem, specifically when posting styles. I was able to solve the problem by making sure any values for "kind" in the style were namespaced, which the examples in the docs don't always get right -- for example:
我有相同的400 Bad Request问题,特别是在发布样式时。我能够通过确保样式中“kind”的任何值都是命名空间来解决问题,而文档中的示例并不总是正确的 - 例如:
{
...
"kind": "fusiontables#buckets"
...
}
Instead of just "buckets".
而不仅仅是“水桶”。