3

我正在尝试让亚马逊 MWS Scratchpad 正常工作,但它不断给我一条消息:

我们计算的请求签名与您提供的签名不匹配。检查您的 AWS 秘密访问密钥和签名方法。有关详细信息,请参阅服务文档。

我在这里寻找类似的主题,但没有什么真正有用的。所以,这里是代码:

$params = array(
    'AWSAccessKeyId'   => AWS_ACCESS_KEY_ID,
    'Action'           => "GetLowestOfferListingsForASIN",
    'SellerId'         => MERCHANT_ID,
    'SignatureMethod'  => "HmacSHA256",
    'SignatureVersion' => "2",
    'Timestamp'        => gmdate("Y-m-d\TH:i:s\Z", time()),
    'Version'          => "2011-10-01",
    'MarketplaceId'    => MARKETPLACE_ID,
    'ItemCondition'    => "new",
    'ASINList.ASIN.1'  => "B001T6OP32");

$url = array();

foreach($params as $key => $val){   
    $val   = str_replace('%7E', '~', rawurlencode($val));
    $url[] = $key . '=' . $val;     
}

$uri = implode('&', $url);

$string_to_sign  = 'POST';
$string_to_sign .= "\n";
$string_to_sign .= 'mws.amazonservices.co.uk' . "\n";
$string_to_sign .= '/Products/2011-10-01' . "\n";
$string_to_sign .= $uri;

$signature = hash_hmac("sha256", $string_to_sign, AWS_SECRET_ACCESS_KEY, TRUE);
$signature = base64_encode($signature);
$signature = urlencode($signature);
$signature = str_replace("%7E", "~", $signature);

$url  = 'https://mws.amazonservices.co.uk/';
$url .= 'Products/2011-10-01' . '?' . $uri . "&Signature=" . $signature;

我敢打赌,问题出在签名上,当我用它打印它时,print $signature它总是包含%符号,当我与Amazon Scratchpad请求详细信息页面进行比较时,SHA 256 HMAC字段 - 没有。

也许有什么我看不到的东西?我正在检查 Secret Access Key 中的空格,看起来没问题。

非常感谢。

4

2 回答 2

4

工作版本:

$param = array();
$param['AWSAccessKeyId']   = AWS_ACCESS_KEY_ID; 
$param['Action']           = 'GetLowestOfferListingsForASIN'; 
$param['SellerId']         = MERCHANT_ID; 
$param['SignatureMethod']  = 'HmacSHA256'; 
$param['SignatureVersion'] = '2'; 
$param['Timestamp']        = gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time());
$param['Version']          = '2011-10-01'; 
$param['MarketplaceId']    = MARKETPLACE_ID; 
$param['ItemCondition']    = 'new';
$param['ASINList.ASIN.1']  = << ITEM ASIN >>;

$url = array();
foreach ($param as $key => $val) {

    $key = str_replace("%7E", "~", rawurlencode($key));
    $val = str_replace("%7E", "~", rawurlencode($val));

    $url[] = "{$key}={$val}";
}

sort($url);

$arr   = implode('&', $url);

$sign  = 'GET' . "\n";
$sign .= 'mws.amazonservices.co.uk' . "\n";
$sign .= '/Products/2011-10-01' . "\n";
$sign .= $arr;

$signature = hash_hmac("sha256", $sign, AWS_SECRET_ACCESS_KEY, true);
$signature = urlencode(base64_encode($signature));

$link  = "https://mws.amazonservices.co.uk/Products/2011-10-01?";
$link .= $arr . "&Signature=" . $signature;

加载$linkcurl 和 vualia!

于 2014-02-27T20:06:56.153 回答
2

要开始从 PHP 访问亚马逊 MWS,您可以下载您有兴趣使用的亚马逊 MWS API 的客户端库,例如您引用的产品 API

从目录中选择一个示例src/MarketplaceWebServiceProducts/Samples,填写您的唯一值,然后运行它。

这些示例使用演示如何成功签署请求的MarketplaceWebServiceProducts_Client类来实现身份验证。src/MarketplaceWebServiceProducts/Client.php

于 2013-09-24T05:46:56.513 回答