Class: MuffinMan::Lwa::AuthHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/muffin_man/lwa/auth_helper.rb

Constant Summary collapse

ACCESS_TOKEN_URL =
"https://api.amazon.com/auth/o2/token".freeze

Class Method Summary collapse

Class Method Details

.get_refresh_token(client_id, client_secret, auth_code) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/muffin_man/lwa/auth_helper.rb', line 5

def self.get_refresh_token(client_id, client_secret, auth_code)
  body = {
    grant_type: "authorization_code",
    code: auth_code,
    client_id: client_id,
    client_secret: client_secret
  }
  response = Typhoeus.post(
    ACCESS_TOKEN_URL,
    body: URI.encode_www_form(body),
    headers: {
      "Content-Type" => "application/x-www-form-urlencoded;charset=UTF-8"
    }
  )
  if response.code != 200
    error_body = JSON.parse(response.body)
    error = "#{error_body["error"]}: #{error_body["error_description"]}"
    raise MuffinMan::Error, error
  end
  JSON.parse(response.body)["refresh_token"]
end