Fusion Tables: why I keep getting the “400 Bad Request” error when I try to update the table style with Ruby RestClient gem

I am trying to update the style for one of my Fusion tables using Rubi gem RestClient.

Here is 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 this 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 enter the update code into the official PUT maker thingie style user style , the update works. But this does not work when I run my Ruby code.

Does anyone know what I'm doing wrong?

EDIT: additional output that I get from adding to RestClient.log = logger

 RestClient.put "https://www.googleapis.com/fusiontables/v1/tables/<MY TABLE ID HERE>/styles/4?key=<AND HERE 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
source share
6 answers

You really should use the google-api-ruby-client library instead of creating your own REST calls. The library abstracts a lot of OAuth stuff and formats the parameters for you.

Having said that, can you turn on debugging for your RestClient and post the output of the RestClient call along with exiting the Google PUT maker spokesperson (I like yours, there is technical jargon)? A comparison of the two should show how they differ and what Google does not like with yours.

+4
source

When answering a question about setting up a registrar without Rails, in the comments to @Jay Lee answer ...

Here a logger is installed for output to standard output:

 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 in the console (e.g. IRB) and add to the last line:

 RestClient.log = logger 

and you should get useful information displayed on the terminal. For more information on the levels available, see the docs for the Logger class .

+4
source

I think the problem is that you are not setting the content type to application / json. Try to do something like this:

 RestClient.put(table_url, update, {"Authorization" => "Bearer #{token}", "Content-type" => "application/json"}) 

The payload in this case should be json, so you can use your json string from your example or run to_json in your data structure.

+3
source

It could be because your hash is here

 update = '{"polygonOptions": {"strokeColor":"#ffffff"}}' 

it should be

 update = {"polygonOptions" => {"strokeColor" => "#ffffff"}} 

Good luck

+2
source

FYI,

Alternatives:

1) Delete the HTTPS request in the project settings and token regeneration.

2) Try using SSL in this case.

3) Sometimes this error occurs when the values ​​exceed more than 255 characters, which may be here with you. The same problem arose once with someone and was solved after debugging. See the link for more details.

+1
source

I had a problem with 400 400 errors, especially when posting styles. I was able to solve the problem by making sure that any values ​​for the “view” in the style were placed in names that do not always correspond to examples in the documents, for example:

 { ... "kind": "fusiontables#buckets" ... } 

Instead of a bucket.

0
source

All Articles