我们计算的请求签名与AMAZON AWS PHP不匹配

时间:2013-02-09 13:55:37

标签: php api amazon-web-services amazon

我已经根据堆栈溢出问题查看了大多数代码示例,但我仍然无法获得请求。我一直收到这个错误:

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

这是我的代码:

$access_key = "ACCESS_KEY";
$associateTag = "AOSSOCIATE_TAG";
$secretkey = "SECRET_KEY";
$keywords = "harry%20potter";
$timestamp = gmdate("Y-m-d\TH:i:s\Z");
$operation = "AWSECommerceService";

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

$signature = createSignature ($operation,$timestamp,$secretkey);

$APIcall = 
"http://ecs.amazonaws.com/onca/xml?".
"AWSAccessKeyId=$access_key&".
"AssociateTag=$associateTag&".
"BrowseNode=1000&".
"ItemPage=1&".
"Keywords=$keywords&".
"Operation=ItemSearch&".
"ResponseGroup=Medium&".
"SearchIndex=Books&".
"Service=AWSECommerceService&".
"Timestamp=$timestamp&".
"Version=2011-08-01&".
"Signature=$signature";

$response = simplexml_load_file($APIcall);

有人可以帮忙吗?

3 个答案:

答案 0 :(得分:2)

我有这个问题很长时间,这对我来说很有用:

require_once 'Crypt/HMAC.php';
require_once 'HTTP/Request.php';

$keyId = "adasdasd";
$secretKey = "asdasdasdasdasd+";

function hex2b64($str) {
    $raw = '';
    for ($i=0; $i < strlen($str); $i+=2) {
    $raw .= chr(hexdec(substr($str, $i, 2)));
    }
    return base64_encode($raw);
}

function constructSig($str) {
    global $secretKey;
    $str = utf8_encode($str);   
    $secretKey = utf8_encode($secretKey);   
    $hasher =& new Crypt_HMAC($secretKey, "sha1");
    $signature = hex2b64($hasher->hash($str));      
    return ($signature);
} 


 $expire = time()+1000;
 $resource = "/demo/files/clouds.jpg";
 $date = gmdate("D, d M Y G:i:s T");
 $mime = "image/jpeg";

 $stringToSign = "PUT\n";
 $stringToSign .= "\n";
 $stringToSign .= "$mime\n"; 
 $stringToSign .= "$date\n";
 $stringToSign .= $resource;
 $req =& new HTTP_Request("http://nameofmine.s3.amazonaws.com/files/clouds.jpg");
 $req->setMethod("PUT"); 
 $req->addHeader("Date",$date);
 $req->addHeader("Authorization", "AWS " . $keyId . ":" . constructSig($stringToSign));
 $req->addHeader("Content-Type",$mime);  
 $req->setBody(file_get_contents($file_path));
 $req->sendRequest();
 $responseCode = $req->getResponseCode();
 $responseString = $req->getResponseBody();
 echo $responseCode;

如您所见,您必须使用加密,HTTP梨插件

答案 1 :(得分:0)

该功能似乎没问题(它与amazon AWS SDK中使用的功能相同),因此请确保前面或复制后的密钥后面没有空格。

答案 2 :(得分:0)

当我手动输入凭据时,我几次收到同样的错误。

然后我尝试使用Console for Windows,这样我就可以复制/粘贴我的凭据。这删除了错误消息。无论是我在打字时吮吸,还是在阅读时吮吸。

长话短说:不要手工输入,复制和过去凭据以避免拼写错误。

编辑: 我的问题是尝试通过EB CLIx3添加我的凭据。