|
require 'oauth2' |
|
require 'faraday' |
|
require 'faraday_middleware' |
|
|
|
# Initialize |
|
|
|
options = { |
|
site: 'https://api.freee.co.jp', |
|
authorize_url: '/oauth/authorize', |
|
token_url: '/oauth/token' |
|
} |
|
|
|
client = OAuth2::Client.new(YOUR_APP_ID, YOUR_SECRET, options) do |conn| |
|
conn.request :url_encoded |
|
conn.request :json |
|
conn.response :json, content_type: /\bjson$/ |
|
conn.adapter Faraday.default_adapter |
|
end |
|
|
|
# Getting authorized |
|
|
|
params = { |
|
grant_type: 'authorization_code', |
|
code: AUTHORIZATION_CODE, |
|
redirect_uri: YOUR_REDIRECT_URI, |
|
headers: { |
|
'Content-Type' => 'application/json', |
|
'Authorization' => HTTPAuth::Basic.pack_authorization(YOUR_APP_ID, YOUR_SECRET) |
|
} |
|
} |
|
|
|
token = client.get_token(params).token |
|
|
|
access_token = OAuth2::AccessToken.new(client, token) |
|
|
|
# GET /api/1/users/me |
|
|
|
access_token.get('/api/1/users/me?companies=true').response.env[:body] |
|
# { |
|
# "user" => |
|
# { |
|
# "email" => "[email protected]", |
|
# "display_name" => "freee太郎, |
|
# "first_name" => "太郎", |
|
# "last_name" => "freee", |
|
# "first_name_kana" => "たろう", |
|
# "last_name_kana"=> "ふりー" |
|
# "companies" => |
|
# { |
|
# "id" => "12345", |
|
# "display_name" => "freee株式会社", |
|
# "role" => "default" |
|
# } |
|
# } |
|
# } |
|
|
|
# GET /api/1/companies/:id |
|
|
|
access_token.get('/api/1/companies/12345?details=true').response.env[:body] |
|
# { |
|
# "company" => |
|
# { |
|
# "id" => 12345, |
|
# "name" => "freee株式会社, |
|
# "name_kana" => "ふりーかぶしきがいしゃ", |
|
# "display_name"=> "freeeカブシキガイシャ", |
|
# "role" => "default", |
|
# "account_items" => {...}, |
|
# "taxes" => {...}, |
|
# "items" => {...}, |
|
# "partners" => {...} |
|
# } |