亚马逊产品 API 返回“SignatureDoesNotMatch"; [英] Amazon Product API returns "SignatureDoesNotMatch"

查看:152
本文介绍了亚马逊产品 API 返回“SignatureDoesNotMatch";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写应用程序以通过 ISBN(用于慈善计划)获取书籍封面.所以我决定使用亚马逊产品 API.我得到了访问密钥和秘密密钥.我得到了一个生成密钥的代码,我传递了 URL,但返回的结果是这样的

I want to write app for get book covers through ISBN (for charity programme). So I decided to use Amazon product API. I got access key and secret key. I got a code for generate secret key I passed URL but that returns like that

<?xml version="1.0"?>
<ItemLookupErrorResponse xmlns="http://ecs.amazonaws.com/doc/2005-10-05/">
<Error><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>6c60b8b7-8b78-4f21-bb7c-3d5d3a26dc48</RequestID>
</ItemLookupErrorResponse>

这是代码

<?php
$AWSAccessKeyId = "*******ACCESS KEY***************";



    $signature = base64_encode(hash_hmac('sha256',$string_to_sign,"******SECRET KEY*******",true));
    // encode any plus (+), equal (=), or other reserved characters in $signature
    $signature = str_replace('%7E','~',rawurlencode($signature));

    $url = "http://ecs.amazonaws.com/onca/xml?AWSAccessKeyId=".$AWSAccessKeyId."&IdType=ASIN&ItemId=1933988355&Operation=ItemLookup&ResponseGroup=Medium%2COffers&Service=AWSECommerceService&Timestamp=".gmdate('Y-m-d\TH:i:s\Z')."&Signature=".$signature;

echo $url;
?>

代码有什么问题?

我尝试使用相同的时间戳.仍然没有运气.这是示例代码.

I tried using same time stamp. Still no luck. That's sample code.

$method = "GET";
$host = "ecs.amazonaws.".$region;
$uri = "/onca/xml";

// additional parameters
$params["Service"] = "AWSECommerceService";
$params["AWSAccessKeyId"] = $public_key;
// GMT timestamp
$params["Timestamp"] = gmdate("Y-m-d\TH:i:s\Z");
// API version
$params["Version"] = "2009-03-31";

// sort the parameters
ksort($params);

// create the canonicalized query
$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);

// create the string to sign
$string_to_sign = $method."\n".$host."\n".$uri."\n".$canonicalized_query;

// calculate HMAC with SHA256 and base64-encoding
$signature = base64_encode(hash_hmac("sha256", $string_to_sign, $private_key, True));

// encode the signature for the request
$signature = str_replace("%7E", "~", rawurlencode($signature)); 

推荐答案

从时间戳创建一个变量,在 $string_to_sign 和 url-parameter 中使用,否则可能会发生两个时间戳不同的情况.为了提供更多帮助,您需要展示 $string_to_sign 的创建过程.

Create a variable from the timestamp to use it inside $string_to_sign and the url-parameter, otherwise it may happen that both timestamps differ. To provide more help you'll need to show the creation of $string_to_sign.

此外:时间戳参数也需要进行 urlencoded.

Furthermore: the Timestamp-parameter needs to be urlencoded too.

也许这对您有用:http://mierendo.com/software/aws_signed_query/

这篇关于亚马逊产品 API 返回“SignatureDoesNotMatch";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆