LoginSignup
2
0

More than 3 years have passed since last update.

備忘録 会計freeeAPI叩いた時ハマったこと

Last updated at Posted at 2020-07-02

GETでクエリ指定する場合は、ちゃんとuri.request_uriでhttp通信しようね

これに尽きます、会計freeeのAPI仕様とかじゃなくただ単に自分の凡ミスだった。

rubyで叩いた

OAuth(オーオース)2.0でACCESS_TOKENは取得済み。
こんな感じでパラメーターをセットして

BASE_URL = 'https://api.freee.co.jp'
MY_COMPANY_ID = '*********'
USE_ACCESS_TOKEN_HEADER = {
  "accept" => "application/json",
  "Authorization"=> "Bearer *************************"
}

以下のように取引先の詳細を取得するAPIを叩いたところ
(分かりやすくかなり単純なメソッドにしてます)

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株式会社')

このようなレスポンスになった。

{"status_code"=>400, "errors"=>[{"type"=>"validation", "messages"=>["company_id が指定されていません。"]}]}

company_id指定してるのになぁ〜
なんでだろうなー
かなり考え込んでしまいました。

原因

この一文

response = http.get(uri.path, headers)

uri.pathのみでhttp通信を行なっていた。
クエリ設定してるのに使ってないじゃん、、、
正しくは

response = http.get(uri.request_uri, headers)

GETでurlにパラメーター埋め込む時は、ちゃんと使わないとですね、、、

日々精進

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0