2016-08-08 2 views
-1

AMAZON WEB SERVICES 용 PHP 코드를 작성했습니다. 이것은 내 코드입니다.Amazon Web Services에서 서명이 일치하지 않습니다.

<?php 

function amazonEncode($text) { 
    $encodedText = ""; 
    $j = strlen($text); 
    for ($i = 0; $i < $j; $i++) { 
     $c = substr($text, $i, 1); 
     if (!preg_match("/[A-Za-z0-9-_.~]/", $c)) { 
      $encodedText .= sprintf("%%%02X", ord($c)); 
     } else { 
      $encodedText .= $c; 
     } 
    } 
    return $encodedText; 
} 

function amazonSign($url, $secretAccessKey) { 
    // 0. Append Timestamp parameter 
    $url .= "&Timestamp=" . gmdate("Y-m-dTH:i:sZ"); 
    // 1a. Sort the UTF-8 query string components by parameter name 
    $urlParts = parse_url($url); 
    parse_str($urlParts["query"], $queryVars); 
    ksort($queryVars); 
    // 1b. URL encode the parameter name and values 
    $encodedVars = array(); 
    foreach ($queryVars as $key => $value) { 
     $encodedVars[amazonEncode($key)] = amazonEncode($value); 
    } 
    // 1c. 1d. Reconstruct encoded query 
    $encodedQueryVars = array(); 
    foreach ($encodedVars as $key => $value) { 
     $encodedQueryVars[] = $key . "=" . $value; 
    } 
    $encodedQuery = implode("&", $encodedQueryVars); 
    // 2. Create the string to sign 
    $stringToSign = "GET"; 
    $stringToSign .= "n" . strtolower($urlParts["host"]); 
    $stringToSign .= "n" . $urlParts["path"]; 
    $stringToSign .= "n" . $encodedQuery; 
    // 3. Calculate an RFC 2104-compliant HMAC with the string you just created, 
    // your Secret Access Key as the key, and SHA256 as the hash algorithm. 
    if (function_exists("hash_hmac")) { 
     $hmac = hash_hmac("sha256", $stringToSign, $secretAccessKey, TRUE); 
    } elseif (function_exists("mhash")) { 
     $hmac = mhash(MHASH_SHA256, $stringToSign, $secretAccessKey); 
    } else { 
     die("No hash function available!"); 
    } 
    // 4. Convert the resulting value to base64 
    $hmacBase64 = base64_encode($hmac); 
    // 5. Use the resulting value as the value of the Signature request parameter 
    // (URL encoded as per step 1b) 
    $url .= "&Signature=" . amazonEncode($hmacBase64); 
    echo $url; 
} 

$url = 'http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=something&AssociateTag=something&Operation=ItemSearch&Keywords=Mustang&SearchIndex=Blended&Condition=Collectible&Timestamp=2016-08-08T12%3A00%3A00Z&Version=2013-08-01'; 
$SECRET_KEY = 'my_secret_key'; 
$url = amazonSign($url, $SECRET_KEY); 
?> 

이 코드는 나에게 URL을 반환합니다. 브라우저에서 해당 URL을 사용하여 검색 결과를 얻을 수 있지만 해당 URL을 사용하면이 오류가 발생합니다.

SignatureDoesNotMatch 우리가 계산 한 요청 서명이 귀하가 제공 한 서명과 일치하지 않습니다. AWS Secret Access Key 및 서명 방법을 확인하십시오. 자세한 내용은 서비스 설명서를 참조하십시오.

나는 이것을 AWSAccessKeyIdSECRET_KEY으로 사용하고 있습니다.

enter image description here

답변

1

아마 $stringToSign .= "n"$stringToSign .= "\n"해야하지만이 유일한 문제가되지 않을 수도 있습니다. 사용자 정의 스크립트에 의존하지 않고 Amazon에서 official PHP SDK을 사용하면 문제가 줄어 듭니다.

+0

안녕하세요. 나는 그것을 바꿨지만 효과는 없었다. –

0

표시되는 오류는 대개 잘못된 액세스 키 또는 비밀 키입니다. 이거나 문제가 UTF-8이 아닌 인코딩 된 문자열 일 수 있습니다. UTF-8로 인코딩하면 오류가 사라집니다. 또는 값이 비어있는 메타 데이터를 보내는 경우 작동하지 않는 것보다 이거나 Content-Length 매개 변수를 제공하지 않으면 그러한 종류의 문제가 발생할 수 있습니다.

관련 문제