2013-02-09 77 views
1

我已經查看了基於此堆棧溢出問題的大多數代碼示例,但仍無法獲得工作請求。我不斷收到此錯誤:我們計算的請求籤名不匹配AMAZON AWS PHP

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

誰能幫助?

+0

爲什麼地球上有這樣的麻煩,當有一個完美的良好支持的PHP API可以直接從亞馬遜使用? http://aws.amazon.com/sdkforphp/ – ceejayoz 2013-02-11 17:50:53

+0

因爲我覺得安裝SDK有點超出了我想達到的目的,所以我最終只是想根據關鍵字在側邊欄中顯示來自亞馬遜的3個項目就是這樣,只是覺得自己做這件事會更笨重和更快。 – AJFMEDIA 2013-02-11 18:40:21

+1

安裝SDK需要幾分鐘時間。你已經明確地花費了至少那試圖自己做,解決問題,並張貼在這裏。 – ceejayoz 2013-02-11 18:42:39

回答

0

該功能似乎沒有問題(與亞馬遜AWS SDK中使用的功能相同),請確保前面沒有空白或複製密鑰後面沒有空白。

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梨插件

0

當我在我的憑據類型手工,我幾次得到同樣的錯誤。

然後我試着用於Windows的控制檯,這樣我就可以複製/粘貼我的憑據。這刪除了錯誤消息。要麼是打字,要麼是讀書。

長話短說:不要用手輸入,複製和過去的憑據以避免輸入錯誤。

編輯: 我的問題是當試圖通過EB CLIx3添加我的憑據。

相關問題