我遇到了很多麻烦和挫折,试图建立与Amazon MWS中的“订单”部分相关的API调用。当我使用Amazon MWS Scratchpad时,我能够得到成功的响应,但是当我自己进行调用时,会出现错误:
我可能错了,但我认为我的调用方式可能有问题,因为即使我使用Scratchpad创建的确切调用,我仍然收到相同的错误。
无论如何,这是我试图构建请求的代码:
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.
我可能错了,但我认为我的调用方式可能有问题,因为即使我使用Scratchpad创建的确切调用,我仍然收到相同的错误。
无论如何,这是我试图构建请求的代码:
<?php
$secretKey = '<MY SECRET KEY>';
$parameters = array();
// required parameters
$parameters['Action'] = 'ListOrders';
$parameters['AWSAccessKeyId'] = '<MY ACCESS KEY>';
$parameters['MWSAuthToken'] = '<MY AUTH TOKEN>';
$parameters['SellerId'] = '<MY SELLER ID>';
$parameters['SignatureMethod'] = 'HmacSHA256';
$parameters['SignatureVersion'] = '2';
$parameters['Timestamp'] = gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time());
$parameters['Version'] = '2013-09-01';
// optional parameters
$parameters['MarketplaceId.Id.1'] = '<MARKETPLACE ID>';
$parameters['CreatedAfter'] = '2015-10-04T04%3A00%3A00Z';
/**
* Calculate String to Sign
*
* @param array $parameters request parameters
* @return String to Sign
*/
function _calculateStringToSign(array $parameters) {
$data = "POST\n";
$data .= "mws.amazonservices.com\n";
$data .= "/Orders/2013-09-01\n";
$data .= _getParametersAsString($parameters);
return $data;
}
/**
* Convert paremeters to Url encoded query string
*/
function _getParametersAsString(array $parameters)
{
uksort($parameters, 'strcmp');
$queryParameters = array();
foreach ($parameters as $key => $value) {
$queryParameters[] = $key . '=' . _urlencode($value);
}
return implode('&', $queryParameters);
}
function _urlencode($value) {
return str_replace('%7E', '~', rawurlencode($value));
}
/**
* Computes RFC 2104-compliant HMAC signature.
*/
function _sign($stringToSign, $secretKey)
{
$hash = 'sha256';
return urlencode(base64_encode(
hash_hmac($hash, $stringToSign, $secretKey, true)
));
}
/**
* Builds up the request.
*/
function buildRequest(array $parameters, $secretKey) {
$endpoint = 'https://mws.amazonservices.com/Orders/2013-09-01';
$signature = _sign(_calculateStringToSign($parameters), $secretKey);
$parameters['Signature'] = $signature;
uksort($parameters, 'strcmp');
return $endpoint . '?' . _getParametersAsString($parameters);
}
这里是实际进行调用的代码:
$request = buildRequest($parameters, $secretKey);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) chrome/39.0.2171.71 Safari/537.36');
$page = curl_exec($ch);
curl_close($ch);
var_dump($page);
我需要帮助确定这个脚本出了什么问题,为什么我无法得到成功的响应。