Make a signed query with Amazon API MWS

ε祈祈猫儿з 提交于 2019-12-02 13:46:22

问题


I'm trying to make a signed request for Seller Amazon Web Services (MWS). I'm using a script from here: Converting amazon MWS scratchpad queries to API calls

But I don't know why I've got an error: "The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details."

So here is my script:

define ("AWS_ACCESS_KEY_ID", "xxxxx");
define ("MERCHANT_ID", "xxxxx");
define ("MARKETPLACE_ID", "xxxxx");
define ("AWS_SECRET_ACCESS_KEY","xxxxx");

$base_url = "https://mws.amazonservices.fr/Products/2011-10-01";
$method = "POST";
$host = "mws.amazonservices.fr";
$uri = "/Products/2011-10-01";

function amazon_xml($searchTerm) {

$params = array(
'AWSAccessKeyId' => AWS_ACCESS_KEY_ID,
'Action' => "GetLowestOfferListingsForSKU",
'SellerId' => MERCHANT_ID,
'SignatureMethod' => "HmacSHA256",
'SignatureVersion' => "2",
'Timestamp'=> date("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time()),
'Version'=> "2011-10-01",
'MarketplaceId' => MARKETPLACE_ID,
'Query' => $searchTerm,
'ItemCondition'=> "New",
'ExcludeMe' => "false");


// Sort the URL parameters
$url_parts = array();
foreach(array_keys($params) as $key)
$url_parts[] = $key . "=" . str_replace('%7E', '~', rawurlencode($params[$key]));
sort($url_parts);

// Construct the string to sign
$url_string = str_replace("%7E", "~", implode("&", $url_parts));
$string_to_sign = "POST\nmws.amazonservices.fr\n/Products/2011-10-01\n" . $url_string;

// Sign the request
$signature = hash_hmac('sha256', $string_to_sign, AWS_SECRET_ACCESS_KEY, TRUE);

// Base64 encode the signature and make it URL safe
$signature = rawurlencode(base64_encode($signature));

$url = "https://mws.amazonservices.fr/Products/2011-10-01" . '?' . $url_string . "&Signature=" . $signature;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$response = curl_exec($ch);

$parsed_xml = simplexml_load_string($response);

return ($parsed_xml);
}

I don't understand what's going wrong If someone could help....

Thanks in advance !


回答1:


You got an issue when you are signing the string..try this

$url_parts = array();
foreach(array_keys($params) as $key)
    $url_parts[] = $key . "=" . str_replace('%7E', '~', rawurlencode($params[$key]));
sort($url_parts);

// Construct the string to sign
$url_string = implode("&", $url_parts);
$string_to_sign = "GET\nmws.amazonservices.com\n/Products/2011-10-01\n" . $url_string;

// Sign the request
$signature = hash_hmac("sha256", $string_to_sign, AWS_SECRET_ACCESS_KEY, TRUE);

// Base64 encode the signature and make it URL safe
$signature = urlencode(base64_encode($signature));

$url = "https://mws.amazonservices.com/Products/2011-10-01" . '?' . $url_string . "&Signature=" . $signature;

its working for me...Hopes this helps




回答2:


Make sure that when using cURL you set the method to "GET", in the above example change to

$method = "GET";

and it should work.



来源:https://stackoverflow.com/questions/14217097/make-a-signed-query-with-amazon-api-mws

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!