使用Amazon API MWS创建签名查询

时间:2013-01-08 14:27:07

标签: php api amazon amazon-mws

我正在尝试为卖家亚马逊网络服务(MWS)签署一份签名请求。 我正在使用这里的脚本: Converting amazon MWS scratchpad queries to API calls

但我不知道为什么我有错误: “我们计算的请求签名与您提供的签名不符。请查看您的AWS秘密访问密钥和签名方法。有关详细信息,请参阅服务文档。”

所以这是我的剧本:

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);
}

我不明白出了什么问题 如果有人可以帮忙......

提前致谢!

2 个答案:

答案 0 :(得分:1)

当您签署字符串时遇到问题..这个

$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;

它为我工作......希望这有帮助

答案 1 :(得分:1)

确保在使用cURL时将方法设置为“GET”,在上面的示例中更改为

$ method =“GET”;

它应该有用。