【问题标题】:bad request 400 error in amazon api access亚马逊 api 访问中的错误请求 400 错误
【发布时间】:2016-03-22 09:59:53
【问题描述】:

修改后的代码

foreach($parsed_xml->OperationRequest->Errors->Error as $error){
    echo "Error code: " . $error->Code . "\r\n";
    echo $error->Message . "\r\n";   echo "\r\n";
}
}
function printSearchResults($parsed_xml, $SearchIndex){
    print("<table>");
    if($numOfItems>0){
        foreach($parsed_xml->Items->Item as $current){
            print("<td><font size='-1'><b>".$current->ItemAttributes->Title."</b>");
            if (isset($current->ItemAttributes->Title)) {
                print("<br>Title: ".$current->ItemAttributes->Title);
            } elseif(isset($current->ItemAttributes->Author)) {
                print("<br>Author: ".$current->ItemAttributes->Author);
            } elseif(isset($current->Offers->Offer->Price->FormattedPrice)){
                print("<br>Price:".$current->Offers->Offer->Price->FormattedPrice);
            }else{
                print("<center>No matches found.</center>");
            }
        }
    }
}
if (!isset($params["Timestamp"])) {
    $params["Timestamp"] = gmdate('Y-m-d\TH:i:s\Z');
}
ksort($params);
$pairs = array();
foreach ($params as $key => $value) {
    array_push($pairs, rawurlencode($key)."=".rawurlencode($value));
}
$canonical_query_string = join("&", $pairs);
$string_to_sign = "GET\n".$endpoint."\n".$uri."\n".$canonical_query_string;
$signature = base64_encode(hash_hmac("sha256", $string_to_sign, $aws_secret_key, true));
$request_url =     'http://'.$endpoint.$uri.'?'.$canonical_query_string.'&Signature='.rawurlencode(     $signature);
echo "Signed URL: \"".$request_url."\"";
}
function fetchDataUsingAPI($UniqueBatchId) {
    echo $this->firstpageurl;
    if( isset($_POST['datafetchresume'])){
        $lastbatchdetails = GetTaskLastBatchDetails($_GET['taskid']);
        $UniqueBatchId =  $lastbatchdetails[0];
        $this->firstpageurl = $lastbatchdetails[1];
    }else {
        $this->firstpageurl = $this->ItemSearch($categorySplit[1],$UniqueBatchId);
    }
    SaveTaskLastBatchDetails($_GET['taskid'], $UniqueBatchId, $this- >firstpageurl);
    return $UniqueBatchId;

【问题讨论】:

  • 这不是问题。,。请格式化代码,缩小问题范围,并添加真实的句子。

标签: php api amazon


【解决方案1】:

每个请求都需要计算签名并将其添加到 URL。查看我的 PHP 函数以添加签名。

另外,使用 CURL 而不是 file_get_contents。即使 API 返回了 400 错误,它也返回了 XML。该 XML 可能包含有用的错误消息。如果您使用 CURL,您可以检测到 400 error 并仍然读取返回的页面内容。使用 file_get_contents,400 错误只会返回一个错误。 (我可能不记得了,但你会更喜欢 CURL。)

Check here 了解更多关于Amazon API error code的信息

在 PHP 代码中,我未定义三个变量。 MY_ASSOCIATE_ID、MY_PUBLIC_KEY、MY_PRIVATE_KEY 必须由您使用适当的值定义。两把钥匙是亚马逊给你的。您可以暂时将它们带到这里:

https://portal.aws.amazon.com/gp/aws/securityCredentials

或者查看本页顶部的“我的帐户”菜单并寻找您的安全凭证。据报道,“新的和改进的”系统不适用于此 API,因此我会在您仍然可以的时候从上面的链接中获取您的两个密钥。将它们存放在安全的地方。

关联 ID 可以是任何东西——API 不会检查它。 API 使用您的 URL 参数使用包含在 URL 中的公钥以及与您提供的公钥相关联的文件中的密钥生成签名。您在 URL 中提供的签名必须与他们的签名完全匹配,因此您的密钥和您的 sig 函数必须与他们的完全匹配。

【讨论】:

  • 感谢您的回复,但我没有得到您的 php 代码的任何链接。
  • 我这里没有提供任何 php 代码。我提供了一个解决方案,说明您的代码为何不起作用。
  • 谢谢我也添加了签名功能,但我对 fetchDataUsingAPI 的调用不起作用
  • 你能在这里发布你的完整代码吗?因为我无法像这样弄清楚。
  • @Rashmi 我今天离开办公室。明天会检查它。或给我发电子邮件
猜你喜欢
  • 2017-04-12
  • 1970-01-01
  • 2017-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-23
  • 1970-01-01
相关资源
最近更新 更多