class Peddler::Token

Requests refresh and access tokens that authorize your application to take actions on behalf of a selling partner.

The refresh token allows you to generate access tokens. Access tokens expire one hour after they are issued.

@see developer-docs.amazon.com/sp-api/docs/connecting-to-the-selling-partner-api

Constants

URL

Attributes

client_id[R]
client_secret[R]
options[R]

Public Class Methods

new(client_id: ENV["LWA_CLIENT_ID"], client_secret: ENV["LWA_CLIENT_SECRET"], **options) click to toggle source
# File lib/peddler/token.rb, line 31
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
request(...) click to toggle source
# File lib/peddler/token.rb, line 26
def request(...)
  new(...).request
end

Public Instance Methods

request() click to toggle source
# File lib/peddler/token.rb, line 37
def request
  response = HTTP.post(URL, form: params)

  unless response.status.success?
    message = response.parse["error_description"]
    raise Error.new(message, response)
  end

  response
end

Private Instance Methods

grant_type() click to toggle source
# File lib/peddler/token.rb, line 58
def grant_type
  return if options.key?(:grant_type)

  if options.key?(:refresh_token)
    "refresh_token"
  elsif options.key?(:scope)
    "client_credentials"
  elsif options.key?(:code)
    "authorization_code"
  end
end
params() click to toggle source
# File lib/peddler/token.rb, line 50
def params
  {
    grant_type: grant_type,
    client_id: client_id,
    client_secret: client_secret,
  }.compact.merge(options)
end