Amazon Product API returns "SignatureDoesNotMatch"
I want to write app for get book covers through ISBN (for charity programme). So I decided to use Amazon product API. I got access key and secret key. I got a code for generate secret key I passed URL but that returns like that
<?xml version="1.0"?>
<ItemLookupErrorResponse xmlns="http://ecs.amazonaws.com/doc/2005-10-05/">
<Error><Code>SignatureDoesNotMatch</Code><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></Error><RequestID>6c60b8b7-8b78-4f21-bb7c-3d5d3a26dc48</RequestID>
</ItemLookupErrorResponse>
that's code
<?php
$AWSAccessKeyId = "*******ACCESS KEY***************";
$signature = base64_encode(hash_hmac('sha256',$string_to_sign,"******SECRET KEY*******",true));
// encode any plus (+), equal (=), or other reserved characters in $signature
$signature = str_replace('%7E','~',rawurlencode($signature));
$url = "http://ecs.amazonaws.com/onca/xml?AWSAccessKeyId=".$AWSAccessKeyId."&IdType=ASIN&ItemId=1933988355&Operation=ItemLookup&ResponseGroup=Medium%2COffers&Service=AWSECommerceService&Timestamp=".gmdate('Y-m-d\TH:i:s\Z')."&am开发者_JS百科p;Signature=".$signature;
echo $url;
?>
what's the problem that code?
Update:
I tried using same time stamp. Still no luck. That's sample code.
$method = "GET";
$host = "ecs.amazonaws.".$region;
$uri = "/onca/xml";
// additional parameters
$params["Service"] = "AWSECommerceService";
$params["AWSAccessKeyId"] = $public_key;
// GMT timestamp
$params["Timestamp"] = gmdate("Y-m-d\TH:i:s\Z");
// API version
$params["Version"] = "2009-03-31";
// sort the parameters
ksort($params);
// create the canonicalized query
$canonicalized_query = array();
foreach ($params as $param=>$value)
{
$param = str_replace("%7E", "~", rawurlencode($param));
$value = str_replace("%7E", "~", rawurlencode($value));
$canonicalized_query[] = $param."=".$value;
}
$canonicalized_query = implode("&", $canonicalized_query);
// create the string to sign
$string_to_sign = $method."\n".$host."\n".$uri."\n".$canonicalized_query;
// calculate HMAC with SHA256 and base64-encoding
$signature = base64_encode(hash_hmac("sha256", $string_to_sign, $private_key, True));
// encode the signature for the request
$signature = str_replace("%7E", "~", rawurlencode($signature));
Create a variable from the timestamp to use it inside $string_to_sign and the url-parameter, otherwise it may happen that both timestamps differ. To provide more help you'll need to show the creation of $string_to_sign.
Furthermore: the Timestamp-parameter needs to be urlencoded too.
Maybe this is usefull to you: http://mierendo.com/software/aws_signed_query/
The "signature" for the request includes the request "operation" parameter and a timestamp. Here is a function that should work to create a signature:
function createSignature($operation,$timestamp){
$the_string=$operation.$timestamp;
return base64_encode(hash_hmac("sha256",$the_string,$this->secret_key,true));
}
The timestamp you send in the request should be the same as the one passed to the createSignature function. In other words, don't generate a timestamp twice. Save it to a variable and then use that for both.
Also, here is a PHP class I created to simplify the process of making requests to the API: https://github.com/traviswimer/AmazonProductAPI_SOAPer/blob/master/AmazonApi.php
精彩评论