Module: Freee::Util

Extended by:
Util
Included in:
Util
Defined in:
lib/freee/util.rb

Class Method Summary collapse

Class Method Details

.create_token(**kwarg) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/freee/util.rb', line 3

def self.create_token(**kwarg)

  client_id = kwarg[:client_id]
  secret_key = kwarg[:secret_key]
  client = OAuth2::Client.new(client_id, secret_key, OPTIONS) do |con|
    con.request :url_encoded
    con.request :json
    con.response :json, content_type: /\bjson$/
    con.adapter Faraday.default_adapter
  end

  params = {
    grant_type: 'authorization_code',
    code: kwarg[:authorization_code],
    redirect_uri: kwarg[:redirect_uri],
    headers: {
      'Content-Type' => 'application/json',
      'Authorization' => HTTPAuth::Basic.pack_authorization(
        client_id,
        secret_key
      )
    }
  }

  client.get_token(params).token
end