亚马逊MWS错误SignatureDoesNotMatch

时间:2015-05-22 08:58:49

标签: php amazon-web-services amazon-mws

我们正在尝试访问亚马逊MWS Api,但我们无法让它工作,我们也不知道为什么。 这是我们到目前为止所尝试的:

        require_once('.config.inc.php');
        $base_url = "https://mws.amazonservices.de/Products/2011-10-01";
        $method = "POST";
        $host = "mws.amazonservices.de";
        $uri = "/Products/2011-10-01";

        $params = array(
            'AWSAccessKeyId' => <our Key>,
            'Action' => "GetLowestOfferListingsForASIN",
            'SellerId' => <our ID>,
            'SignatureMethod' => "HmacSHA256",
            'SignatureVersion' => "2",
            'Timestamp'=> gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time()), //tried this with time()+7200 since our server is 2 hours back but we also accessed mws to get the time used there
            'Version'=> "2011-10-01",
            'MarketplaceId' => <our MpID>,
            'ItemCondition' => 'new',
            'ASINList.ASIN.1' => B00NN8LSXY );

            // 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 = implode("&", $url_parts);
            $string_to_sign = "POST\nmws.amazonservices.de\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.de/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);

        echo $response;
            //return ($parsed_xml);

在我们添加的.config.inc.php文件中 所有的钥匙和钥匙ID +

define('APPLICATION_NAME', '<our Firm>');
define('APPLICATION_VERSION', '1.0');

在我们完成所有操作之前,我们检查了MWS-Scratchpad中的所有内容,但一切似乎都在那里工作(在mws.amazon.de上)。

但我们仍然获得SignatureDoesNotMatch错误代码

 <Message>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.</Message>

或此错误代码:

<Message>Request signature is for too far in the future. Timestamp date: 2015-05-23T04:54:38.000Z. Currently, 10 percent of requests that are more than 15 minutes in the future will be rejected.</Message>

希望有人可以帮助我们浏览所有其他帖子和开发者指南 - 这似乎没什么帮助

2 个答案:

答案 0 :(得分:4)

我遇到了同样的问题(等等)。

亚马逊提供的PHP参考和示例的答案深埋在其中。整个事情是烤宽面条代码的一个典型例子,读起来像是对各地编码员的侮辱。

API期望HTTP POST包含所有请求数据以及使用您的密钥进行签名。数组的排序和url编码标准将字符串更改为sign。

亚马逊预计它的排序方式如下:

uksort($params, 'strcmp');

忘记整个$ url_parts部分,这很麻烦。使用http_build_query()代替:

$url_string = http_build_query($params,'','&',PHP_QUERY_RFC3986);

亚马逊期望RFC3986,因此空格编码为'+',而不是'%20'。时间戳也应如下所示:

'Timestamp' => gmdate("Y-m-d\TH:i:s\\Z", time()),
祝你好运。

答案 1 :(得分:3)

当您的机器/服务器时间不正确时会发生这种情况。有时当我重启它时,它会发生在我的服务器上。只需使用时间服务器设置同步。