尝试在Amazon API上列出产品时的SignatureDoesNotMatch

时间:2015-08-09 22:36:27

标签: php amazon-web-services

我想知道是否有人可以提供帮助。

我正试图通过API在亚马逊上列出产品。

使用GetOrders时效果很好,但除了参数之外还有类似的代码我在使用SubmitFeed _POST_PRODUCT_DATA _

时会收到以下错误消息

" Sender SignatureDoesNotMatch我们计算的请求签名与您提供的签名不匹配。检查您的AWS Secret Access Key和签名方法。有关详细信息,请参阅服务文档。"

我的所有细节都是正确的,密钥,aws访问密钥等。我已将我的代码中的字符串与在Amazon API测试工具中生成的字符串进行比较,它们完全相同所以我是不确定是什么问题。

以下是我正在使用的代码 -

$timestamp = date('c', strtotime($todays_date_time));
$timestamp = gmdate('Y-m-d\TH:i:s\Z', strtotime($timestamp));

$params = array(
    'AWSAccessKeyId' => "MY_AWS_KEY",
    'Action' => "SubmitFeed",
    'Merchant' => "MY_SELLER_ID",
    'FeedType' => "_POST_PRODUCT_DATA_",
    'SignatureMethod' => "HmacSHA256",
    'SignatureVersion' => "2",
    'Timestamp'=> $timestamp,
    'Version'=> "2009-01-01",
    'MarketplaceIdList.Id.1' => "MY_MARKETPLACE_ID",
    'PurgeAndReplace'=>'false'
);

$secret = 'MY_SECRET_KEY';

$url_parts = array();

foreach(array_keys($params) as $key) {

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

}

然后在这里我创建XML并将其存储在变量$ amazon_feed中然后 -

sort($url_parts);

$url_string = implode("&", $url_parts);
$string_to_sign = "POST\nmws.amazonservices.co.uk\n/\n" . $url_string;

$signature = hash_hmac("sha256", $string_to_sign, $secret, TRUE);

$http_header     =   array();
$http_header[]   =   'Transfer-Encoding: chunked';
$http_header[]   =   'Content-Type: application/xml';
$http_header[]   =   'Content-MD5: ' . base64_encode(md5($amazon_feed, true));
$http_header[]   =   'Expect:';
$http_header[]   =   'Accept:';

$signature = urlencode(base64_encode($signature));

$link = "https://mws.amazonservices.co.uk/Feeds/2009-01-01?".$url_string."&Signature=".$signature;

$ch = curl_init($link);
curl_setopt($ch, CURLOPT_HTTPHEADER, $http_header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $amazon_feed); 
$response = curl_exec($ch);

print_r($response);

$info = curl_getinfo($ch);
curl_close($ch);

有人能帮忙吗?

1 个答案:

答案 0 :(得分:1)

在你传递它们之前,你必须ksort()你的参数:

foreach(array_keys($params) as $key) {

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

}

e.g。

$params = array(
    'AWSAccessKeyId' => "MY_AWS_KEY",
    'Action' => "SubmitFeed",
    'SellerId' => "MY_SELLER_ID",
    'FeedType' => "_POST_PRODUCT_DATA_",
    'SignatureMethod' => "HmacSHA256",
    'SignatureVersion' => "2",
    'Timestamp'=> $timestamp,
    'Version'=> "2009-01-01",
    'MarketplaceIdList.Id.1' => "MY_MARKETPLACE_ID",
    'PurgeAndReplace'=>'false'
);

$secret = 'MY_SECRET_KEY';
$url_parts = array();
ksort($params);
foreach(array_keys($params) as $key) {

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

}

我不完全确定您要签名的字符串,您应该尝试使用它(将/ Feeds / 2009-01-01添加到其中):

"POST\nmws-eu.amazonservices.com\n/Feeds/2009-01-01\n" . $url_string

此外,亚马逊预计SellerId操作_POST_PRODUCT_DATA_,而不是Merchant

我建议您使用mws-eu-amazonservices.com而不是co.uk,您可以将其用于所有欧洲市场,并且不需要为每个市场更改它。 作为旁注:

亚马逊并没有真正报告错误。如果只有SellerId是像上面这样的商家,或者与您尝试做的事情无关的任何不同,那么您也会遇到错误。