我们计算的请求签名与AMAZON AWS PHP不匹配
我已经查看了大多数基于堆栈溢出问题的代码示例,但仍然无法使请求正常工作。我不断地发现这个错误:我们计算的请求签名与AMAZON AWS PHP不匹配,php,api,amazon-web-services,amazon,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 meth
<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);
有人能帮忙吗?该函数似乎还可以(与amazon AWS SDK中使用的函数相同),因此请确保复制的密钥前后没有空格。我有这个问题很久了,它对我的代码起到了作用:
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;
require_once'Crypt/HMAC.php';
需要一次“HTTP/Request.php”;
$keyId=“adasdasd”;
$secretKey=“asdasd+”;
函数hex2b64($str){
$raw='';
对于($i=0;$ihash($str));
报税表(签名);
}
$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=&新的HTTP\U请求(“http://nameofmine.s3.amazonaws.com/files/clouds.jpg");
$req->setMethod(“PUT”);
$req->addHeader(“日期”,$Date);
$req->addHeader(“授权”,“AWS”。$keyId.”:“.constructSig($stringToSign));
$req->addHeader(“内容类型”,$mime);
$req->setBody(文件获取内容($file\u路径));
$req->sendRequest();
$responseCode=$req->getResponseCode();
$responseString=$req->getResponseBody();
echo$responseCode;
正如您所见,您必须使用加密、HTTP pear插件。当我手动输入凭据时,我有好几次遇到相同的错误 然后我尝试了Windows控制台,以便复制/粘贴我的凭据。这删除了错误消息。要么我打字很烂,要么我看书很烂 长话短说:不要手工打字,复制和过去的凭证,以避免打字错误 编辑:
我的问题是试图通过EB-CLIx3添加我的凭据。既然亚马逊提供了一个非常好的、支持良好的PHP API,你到底为什么要为此烦恼?因为我觉得安装SDK对于我想要实现的目标来说有点过头了,所以我最终只想在一个基于关键字的边栏中显示来自amazon的3个项目,就是这样,我觉得自己做会更简单、更快。安装SDK需要几分钟的时间。很明显,你至少花了那么多时间自己尝试、排除故障并在这里发布。截至2013年9月,amazon有一个可供参考的JS实现: