Money-Code

Coding For Online Success

Amazon Product API: SignatureDoesNotMatch error response

| 7 Comments

On August 15, 2009, all Product Advertising API developers were required to authenticate ALL calls using a hmac/sha256 signing token. For some reason, this was a major pain in the ass for me, but I finally got things to work.

If you’re having trouble, and you’re using PHP5, this may help you.

The first item you’ll need is the ‘secret’. You need to log into the Amazon Web Services area (https://developer.amazonwebservices.com). After logging in (assuming you already have a account if you’re using their API), you need to hover over ‘Your Account’, and click ‘Access Identifiers’. Here you will see ‘Access Key ID’ and ‘Secret Access Key’. We’ll be referring Access Key ID as the public key and Secret Access Key as the private key in a function provided below. Copy these two values and add them to your code somewhere.

You will need to ensure that PHP is built with hash support. You can verify this with by view info (<?phpinfo()?>). If hash support is there we can use the built in hash_hmac() function. The concept is pretty straight forward (eventhough, I’ve been fighting it all night prior to this post). You create a string, and ‘hash’ it with our private key, then we pass the string to Amazon’s API, and they hash the same string with our private key. The value should be the same, and if so, we’re good. If something is off any where in that sequence you’ll be getting the dreaded ‘SignatureDoesNotMatch‘ message in your XML return.

I came across Ulrich Mierendorff’s function for sending a signed AWS request on the developer forum, and it was the only PHP code snippet that worked for me, so I need to pass it on. Below is the function:

span class=”coMULTI”>/*
    Copyright (c) 2009 Ulrich Mierendorff

    Permission is hereby granted, free of charge, to any person obtaining a
    copy of this software and associated documentation files (the "Software"),
    to deal in the Software without restriction, including without limitation
    the rights to use, copy, modify, merge, publish, distribute, sublicense,
    and/or sell copies of the Software, and to permit persons to whom the
    Software is furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
    THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    DEALINGS IN THE SOFTWARE.
    */

    /*
    Parameters:
        $region – the Amazon(r) region (ca,com,co.uk,de,fr,jp)
        $params – an array of parameters, eg. array("Operation"=>"ItemLookup",
                        "ItemId"=>"B000X9FLKM", "ResponseGroup"=>"Small")
        $public_key – your "Access Key ID"
        $private_key – your "Secret Access Key"
    */

    // some paramters
    $method = "GET";
    $host = "ecs.amazonaws.".$region;
    $uri = "/onca/xml";

    // additional parameters
    $params["Service"] = "AWSECommerceService";
    $params["AWSAccessKeyId"// GMT timestamp
    $params["Timestamp""Y-m-d\TH:i:s\Z");
    // API version
    $params["Version"] = "2009-03-31";

    // sort the parameters
// create the canonicalized query
"%7E", "~""%7E", "~""=""&amp;"// create the string to sign
"\n".$host."\n".$uri."\n"// calculate HMAC with SHA256 and base64-encoding
‘sha256’// encode the signature for the request
"%7E", "~"// create request
    $request = "http://".$host.$uri."?""&amp;Signature=".$signature;

    // do request
// parse XML
// no xml

To call the function you create a array of arguments and pass it to the function along with the keys values.

$amazontag      = ‘asdfasfd-20’"asfdasdfasdfasfdasfda""SECRETKEYasfdasdfasdfasfdasfda""Operation"=>"ItemSearch",
                                "IdType"=>"ASIN",
                                "MerchantId"=>"All",
                                "ResponseGroup"=>"Large",
                                "SearchIndex"=>$searchindex,
                                "BrowseNode"=>$BrowseNode,
                                "Keywords"=>$safeQuery,
                                "AWSAccessKeyId""com"

This worked perfectly for me, and got my Amazon sites back on line. Hope this helps!!

7 Comments

Leave a Reply

Required fields are marked *.


CommentLuv badge

This site uses Akismet to reduce spam. Learn how your comment data is processed.