It's all about this, not the API specifications of accounting freee, but just my own mistake.
ACCESS_TOKEN has been acquired with OAuth 2.0. Set the parameters like this
BASE_URL = 'https://api.freee.co.jp'
MY_COMPANY_ID = '*********'
USE_ACCESS_TOKEN_HEADER = {
"accept" => "application/json",
"Authorization"=> "Bearer *************************"
}
I hit the API to get the details of the business partner as follows (It's a fairly simple method that is easy to understand)
def get_torihikisaki_id(name)
uri = URI.parse(BASE_URL + '/api/1/partners')
uri.query = URI.encode_www_form({"company_id" => MY_COMPANY_ID ,'keyword'=> name})
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
headers = USE_ACCESS_TOKEN_HEADER
response = http.get(uri.path, headers)
res_hash = JSON.parse(response.body)
end
puts get_torihikisaki_id('hoge co')
The response was like this.
{"status_code"=>400, "errors"=>[{"type"=>"validation", "messages"=>["company_id is not specified."]}]}
I'm specifying company_id ~ I wonder why I've thought a lot.
This sentence
response = http.get(uri.path, headers)
http communication was performed only with uri.path. I'm not using it to set the query ... Correctly
response = http.get(uri.request_uri, headers)
When embedding parameters in url with GET, you have to use it properly ...
Dedicated every day
Recommended Posts