我们计算的请求签名与您提供的签名不匹配。 - AWS中的错误

时间:2014-11-17 10:06:12

标签: php amazon-web-services

我有以下代码:

$method = "GET";
$host = "payments-sandbox.amazon.com";
$uri = "/cba/api/purchasecontract/";
$private_key='PRIVATE_KEY';
$time = date('c');

$params = array(
    'AWSAccessKeyId' => $private_key,
    'Action' => "CreatePurchaseContract",
    'SignatureMethod' => "HmacSHA256",
    'SignatureVersion' => "2",
    'Timestamp' => $time);

uksort($params, 'strcmp');
$canonicalized_query = array();
foreach ($params as $param => $value) {
    $param = str_replace("%7E", "~", rawurlencode($param));
    $value = str_replace("%7E", "~", rawurlencode($value));
    $canonicalized_query[] = $param . "=" . $value;
}
$canonicalized_query = implode("&", $canonicalized_query);
$string_to_sign = $method . "\n" . $host . "\n" . $uri . "\n" . $canonicalized_query;
$signature = base64_encode(hash_hmac("sha256", $string_to_sign, $private_key, True));
$signature = str_replace("%7E", "~", rawurlencode($signature));

echo $url = "https://payments-sandbox.amazon.com/cba/api/purchasecontract/"
 . "?Action=CreatePurchaseContract"
 . "&SignatureMethod=HmacSHA256"
 . "&AWSAccessKeyId=$private_key"
 . "&Signature=".$signature
 . "&SignatureVersion=2"
 . "&Timestamp=" . $time;

但是当我在新标签页面中打开网址时,它会返回XML:

<ErrorResponse>
    <Error>
        <Type>Sender</Type>
        <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.</Message>
    </Error>
    <RequestId>01a5de72-6e3f-11e4-85b3-9119f1b849cd</RequestId>
</ErrorResponse>

在新标签页中打开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.链接时,会显示$url错误消息。

我不知道创建签名有什么问题?

[UPDATE]

我已尝试cUrl来获取数据:

$ch = curl_init();

$options = array(CURLOPT_URL => $url);
curl_setopt_array($ch, $options);
$data = curl_exec($ch);

if(!$data){
    die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}
curl_close($ch);
print_r($data);

但它给了我Error: "SSL certificate problem: unable to get local issuer certificate" - Code: 60。那么我该做什么来从payments-sandbox.amazon.com

获取数据

1 个答案:

答案 0 :(得分:0)

当我尝试使用EB CLIx3中的eb init命令添加我的凭据时,出现了同样的错误。

事实证明我每次都输错了凭证。我切换到Windows的控制台,我可以复制/粘贴我的凭据。现在一切顺利。

因此,复制/过去凭证是我的建议。如果这不起作用,您可能希望为具有管理员权限的用户创建新的访问密钥。您可以在IAM部分执行此操作。然后再尝试复制过去。