Class: Peddler::LWA

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

Overview

Requests Login with Amazon (LWA) access tokens that authorize your application to make SP-API requests.

Supports three OAuth 2.0 grant types:

  • authorization_code: Exchange authorization code for refresh token (initial authorization)
  • refresh_token: Exchange refresh token for access token (most common)
  • client_credentials: Get access token for grantless operations (e.g., notifications)

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id: ENV["LWA_CLIENT_ID"], client_secret: ENV["LWA_CLIENT_SECRET"], **options) ⇒ LWA

Returns a new instance of LWA.



25
26
27
28
29
# File 'lib/peddler/lwa.rb', line 25

def initialize(client_id: ENV["LWA_CLIENT_ID"], client_secret: ENV["LWA_CLIENT_SECRET"], **options)
  @client_id = client_id
  @client_secret = client_secret
  @options = options
end

Instance Attribute Details

#client_idObject (readonly)

Returns the value of attribute client_id.



17
18
19
# File 'lib/peddler/lwa.rb', line 17

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



17
18
19
# File 'lib/peddler/lwa.rb', line 17

def client_secret
  @client_secret
end

#optionsObject (readonly)

Returns the value of attribute options.



17
18
19
# File 'lib/peddler/lwa.rb', line 17

def options
  @options
end

Class Method Details

.requestObject



20
21
22
# File 'lib/peddler/lwa.rb', line 20

def request(...)
  new(...).request
end

Instance Method Details

#grant_typeObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/peddler/lwa.rb', line 36

def grant_type
  if options.key?(:grant_type)
    options[:grant_type]
  elsif options.key?(:refresh_token)
    "refresh_token"
  elsif options.key?(:scope)
    "client_credentials"
  elsif options.key?(:code)
    "authorization_code"
  end
end

#requestObject



31
32
33
34
# File 'lib/peddler/lwa.rb', line 31

def request
  http_response = HTTP.post(URL, form: params)
  Response.wrap(http_response, parser: -> { LWAToken })
end