Authorization with the amz_sp_api
rubygem.
First you have to handle oauth (user visits with code, you convert into tokens). After oauth is complete you will have a refresh_token
. That token is
def api_client
config = AmzSpApi::SpConfiguration.new
config.client_id = Rails.application.credentials.config[:SPAPI_LWA_CLIENT]
config.client_secret = Rails.application.credentials.config[:SPAPI_LWA_SECRET]
config.refresh_token = resource.refresh_token
config.credentials_provider = Aws::STS::Client.new(
region: AmzSpApi::SpConfiguration::AWS_REGION_MAP['na'],
access_key_id: Rails.application.credentials.config[:SPAPI_AWS_KEY],
secret_access_key: Rails.application.credentials.config[:SPAPI_AWS_SECRET],
).assume_role(role_arn: Rails.application.credentials.config[:SPAPI_AWS_ARN_ROLE], role_session_name: SecureRandom.uuid)
config.region = 'na'
config.timeout = 5 # seconds
# config.access_token_key
config.save_access_token = -> (access_token_key, token) { Rails.cache.write("SPAPI/TOKEN/#{access_token_key}", token[:access_token], expires_in: token[:expires_in] - 60) }
config.get_access_token = -> (access_token_key) { Rails.cache.read("SPAPI/TOKEN/#{access_token_key}") }
AmzSpApi::SpApiClient.new(config)
end
Then each time you want to request something:
require 'catalog-items-api-model'
api_instance = AmzSpApi::CatalogItemsApiModel::CatalogApi.new(api_client)
resp = api_instance.search_catalog_items(marketplace_ids, opts)