亚马逊MWS API返回SignatureDoesNotMatch

时间:2016-04-26 22:20:33

标签: php amazon-mws amazon-product-api

我需要调用亚马逊MWS操作'RequestReport'并将ReportType指定为'_GET_FLAT_FILE_OPEN_LISTINGS_DATA_'。我已成功连接到获取FulfillmentInventory / ListInventorySupply,所以我知道cURL和亚马逊设置是正确的,但每次我提交我得到'我们计算的请求签名与您提供的签名不匹配。检查您的AWS Secret Access Key和签名方法。有关详细信息,请参阅服务文档。从服务器返回。我已经尝试了第23行和第14行的排序和ksort - 在对FulfillmentInventory / ListInventorySupply的调用中,我必须使用两个ksorts进行设置,以便按照API的正确顺序保持SKU列表

这是代码,正如我所说,秘密,商家和keyid是正确的:

header('Content-type: application/xml');
$secret = 'secretcodehere';

$param = array();
$param['AWSAccessKeyId']   = 'accessidhere'; 
$param['Action']           = 'RequestReport'; 
$param['Merchant']         = 'merchantidhere';
$param['SignatureVersion'] = '2'; 
$param['Timestamp']        = gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time());

$param['Version']          = '2009-01-01'; 
$param['SignatureMethod']  = 'HmacSHA256';  
$param['ReportType']  = '_GET_FLAT_FILE_OPEN_LISTINGS_DATA_';  
ksort($param);
$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  = 'POST' . "\n";
$sign .= 'mws.amazonservices.com';
$sign .= '/doc/2009-01-01' . "\n";
$sign .= $arr;

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

$link  = "https://mws.amazonservices.com/doc/2009-01-01/?";
$link .= $arr . "&Signature=" . $signature;
/*

echo($link);//for debugging 

exit(); */

$ch = curl_init($link);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: text/xml'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
print_r($response);

我已经在MWS暂存器中尝试了它并且信息是正确的并且生成200响应,当我检查URL与暂存器生成的URL时它看起来'正确',所以我必须丢失一些东西,我希望它对那里的人来说很明显,因为我感到很困惑。

btw-scratchpad将其列为SellerId,但该网址将其显示为Merchant - 我已经尝试了两者并没有带来欢乐

1 个答案:

答案 0 :(得分:0)

不要向你抛出曲线球,但我在使用RequestReport时唯一的成功就是使用亚马逊创建的PHP库。如果您还没有link

这是我刚刚确认的用于请求报告的代码:

<?php

define('AWS_ACCESS_KEY_ID', $am_aws_access_key);
define('AWS_SECRET_ACCESS_KEY', $am_secret_key);
define('MERCHANT_ID', $am_merchant_id);
define('MARKETPLACE_ID', $am_marketplace_id);

include_once ('/link/to/Amazon/library/MarketplaceWebService/Samples/.config.inc.php');

include_once ('functions.php');

$serviceUrl = "https://mws.amazonservices.com";
$config = array (
    'ServiceURL' => $serviceUrl,
    'ProxyHost' => null,
    'ProxyPort' => -1,
    'MaxErrorRetry' => 3,
);
$service = new MarketplaceWebService_Client(
    AWS_ACCESS_KEY_ID,
    AWS_SECRET_ACCESS_KEY,
    $config,
    APPLICATION_NAME,
    APPLICATION_VERSION);

echo '<br />';
$parameters = array (
    'Marketplace' => MARKETPLACE_ID,
    'Merchant' => MERCHANT_ID,
    'ReportType' => '_GET_FLAT_FILE_OPEN_LISTINGS_DATA_',
);

echo '<br /><br/>Request Report Request:<br><br>';
$request = new MarketplaceWebService_Model_RequestReportRequest($parameters);
print_r($request);
invokeRequestReport($service, $request);
echo '<br /><br/>';

functions.php文件(基本上是MarketplaceWebService\Samples\RequestReportSample.php文件中代码后半部分的重要函数:

function invokeRequestReport(MarketplaceWebService_Interface $service, $request)
{
    try {
        $response = $service->requestReport($request);
        echo ("Service Response\n");
        echo ("=============================================================================\n");
        echo("        RequestReportResponse\n");
        if ($response->isSetRequestReportResult()) {
            echo("            RequestReportResult\n");
            $requestReportResult = $response->getRequestReportResult();
            if ($requestReportResult->isSetReportRequestInfo()) {
                $reportRequestInfo = $requestReportResult->getReportRequestInfo();
                echo("                ReportRequestInfo\n");
                if ($reportRequestInfo->isSetReportRequestId())
                {
                    echo("                    ReportRequestId\n");
                    echo("                        " . $reportRequestInfo->getReportRequestId() . "\n");
                }
                $report_request_id = $reportRequestInfo->getReportRequestId();
                $report_type = '';
                if ($reportRequestInfo->isSetReportType())
                {
                    echo("                    ReportType\n");
                    echo("                        " . $reportRequestInfo->getReportType() . "\n");
                    $report_type = $reportRequestInfo->getReportType();
                }
                if ($reportRequestInfo->isSetStartDate())
                {
                    echo("                    StartDate\n");
                    echo("                        " . $reportRequestInfo->getStartDate()->format(DATE_FORMAT) . "\n");
                }
                if ($reportRequestInfo->isSetEndDate())
                {
                    echo("                    EndDate\n");
                    echo("                        " . $reportRequestInfo->getEndDate()->format(DATE_FORMAT) . "\n");
                }
                if ($reportRequestInfo->isSetSubmittedDate())
                {
                    echo("                    SubmittedDate\n");
                    echo("                        " . $reportRequestInfo->getSubmittedDate()->format(DATE_FORMAT) . "\n");
                }
                if ($reportRequestInfo->isSetReportProcessingStatus())
                {
                    echo("                    ReportProcessingStatus\n");
                    echo("                        " . $reportRequestInfo->getReportProcessingStatus() . "\n");
                }
                if($report_type == '_GET_FLAT_FILE_OPEN_LISTINGS_DATA_') {
                    if(!empty($report_request_id)) {
                        $parameters = array (
                            'Marketplace' => MARKETPLACE_ID,
                            'Merchant' => MERCHANT_ID,
                            'Report' => @fopen('php://memory', 'rw+'),
                            'ReportRequestIdList' => $report_request_id,
                        );
                        $report = new MarketplaceWebService_Model_GetReportRequestListRequest($parameters);
                        print_r($report);
                    }
                }
            }
        }
        if ($response->isSetResponseMetadata()) {
            echo("            ResponseMetadata\n");
            $responseMetadata = $response->getResponseMetadata();
            if ($responseMetadata->isSetRequestId())
            {
                echo("                RequestId\n");
                echo("                    " . $responseMetadata->getRequestId() . "\n");
            }
        }
    } catch (MarketplaceWebService_Exception $ex) {
        echo("Caught Exception: " . $ex->getMessage() . "\n");
        echo("Response Status Code: " . $ex->getStatusCode() . "\n");
        echo("Error Code: " . $ex->getErrorCode() . "\n");
        echo("Error Type: " . $ex->getErrorType() . "\n");
        echo("Request ID: " . $ex->getRequestId() . "\n");
        echo("XML: " . $ex->getXML() . "\n");
    }
}

<强> 修改

以下是.config.inc.php文件的重要部分:

<?php

define ('DATE_FORMAT', 'Y-m-d\TH:i:s\Z');
date_default_timezone_set('America/Denver');

$app_name = "Just make up a name like 'Awesome Sync'";
$app_version = "1.0";
define('APPLICATION_NAME', $app_name);
define('APPLICATION_VERSION', $app_version);

set_include_path('/link/to/Amazon/library/');
...rest of code...

<强> 修改

此代码将创建报告请求,但是,它实际上并未创建报告。您必须继续使用相同的代码轮询Amazon,直到您收到&#34;完成&#34;或类似的东西(无法记住亚马逊在创建报告时发回的确切字词)。然后,您需要实际检索报告。