1

我已经查看了大多数基于堆栈溢出问题的代码示例,但我仍然无法让请求工作。我不断收到此错误:

<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);

任何人都可以帮忙吗?

4

3 回答 3

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;

如您所见,您必须使用 Crypto、HTTP pear 插件

于 2013-02-18T03:02:05.147 回答
0

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

于 2013-02-13T20:13:14.840 回答
0

当我手动输入凭据时,我遇到了几次相同的错误。

然后我尝试了 Windows 控制台,这样我就可以复制/粘贴我的凭据。这删除了错误消息。我要么打字很烂,要么阅读很烂。

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

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

于 2015-02-23T20:56:10.497 回答