Amazon Product API Returns "SignatureDoesNotMatch"

I want to write an application for receiving book covers through ISBN (for a charity program). So I decided to use the Amazon product API. I got a passkey and secret key. I have a code for generating a secret key, which I passed the URL, but which is returned in this way

<?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>

this 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')."&Signature=".$signature;

echo $url;
?>

what's the problem with this code?

Update:

I tried using a timestamp. Not lucky yet. This 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)); 
+3
source share
2 answers

, $string_to_sign url-, , . , $string_to_sign.

: timestamp .

, : http://mierendo.com/software/aws_signed_query/

+6

"" "" . , :

function createSignature($operation,$timestamp){
    $the_string=$operation.$timestamp;
    return base64_encode(hash_hmac("sha256",$the_string,$this->secret_key,true));
}

, , , , createSignature. , . , .

, PHP, API: https://github.com/traviswimer/AmazonProductAPI_SOAPer/blob/master/AmazonApi.php

+2

All Articles