获取 restrictedDataToken
require 'tokens-api-model'
module SpApi::Baseclass TokensApidef initialize(merchant)client = AmzSpApi::SpApiClient.new@api_instance = AmzSpApi::TokensApiModel::TokensApi.new(client)enddef createRestrictedDataToken(order)hash = {"restrictedResources": [{"method": "GET","path": "/orders/v0/orders/#{order.platform_order_number}/address"}, {"method": "GET","path": "/orders/v0/orders/#{order.platform_order_number}/buyerInfo"}]}body = AmzSpApi::TokensApiModel::CreateRestrictedDataTokenRequest.new(hash)beginresult = @api_instance.create_restricted_data_token(body)restrictedDataToken = result.fetch(:restrictedDataToken)rescue AmzSpApi::TokensApiModel::ApiError => eputs "Exception when calling TokensApi->create_restricted_data_token: #{e}"endendend
end
使用restrictedDataToken
module SpApi::Baseclass OrdersApidef initialize(merchant)@client = AmzSpApi::SpApiClient.newenddef get_order_buyer_info_by_order_number(order)@client.config.save_access_token = -> (access_token_key, order) dotoken = SpApi::Base::TokensApi.new(merchant).createRestrictedDataToken(order)Rails.cache.write("SPAPI-RESTRICTED-TOKEN-#{access_token_key}", token[:restrictedDataToken], expires_in: token[:expiresIn] - 60)end@client.config.get_access_token = -> (access_token_key) { Rails.cache.read("SPAPI-RESTRICTED-TOKEN-#{access_token_key}") }@api_instance = AmzSpApi::OrdersApiModel::OrdersV0Api.new(@client)result = @api_instance.get_order_buyer_info(platform_order_number)rescue AmzSpApi::OrdersApiModel::ApiError => eputs "Exception when calling OrdersV0Api->get_order_buyer_info: #{e}"endend
end
- 其本质就是用获取到的 restrictedDataToken 代替由 refresh_token 给https://api.amazon.com/auth/o2/token发请求时候获取的 access_token,作为下个请求头里的 x-amz-access-token 值来告诉亚马逊有权获取受限数据
半手动签名调用api
require 'aws-sigv4'
require 'net/http'module Net::HTTPHeaderdef capitalize(name)nameendprivate :capitalize
endurl = 'https://sellingpartnerapi-eu.amazon.com/sellers/v1/marketplaceParticipations'signer = Aws::Sigv4::Signer.new(access_key_id: access_key_id,region: 'eu-west-1',secret_access_key: secret_access_key,service: 'execute-api',
)signature = signer.sign_request(http_method: 'GET',url: url,headers: {'host' => 'sellingpartnerapi-eu.amazon.com','user-agent' => 'test (Language=Ruby)','x-amz-access-token' => access_token})headers = {'host' => signature.headers['host'],'user-agent' => 'test (Language=Ruby)','x-amz-access-token' => access_token,'x-amz-content-sha256' => signature.headers['x-amz-content-sha256'],'x-amz-date' => signature.headers['x-amz-date'],'authorization' => signature.headers['authorization']
}
ur = URI(url)
http = Net::HTTP.new(ur.host, ur.port)
http.use_ssl = true
res = http.get(ur, headers)
res.body
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!