Amazone API SignatureDoesNotMatch [英] Amazone API SignatureDoesNotMatch

查看:66
本文介绍了Amazone API SignatureDoesNotMatch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi2all

i当我尝试在amzone api中使用itemlookup时出现此错误



  public  '  Code'=>字符串'SignatureDoesNotMatch'(长度= 21) 
public ' 消息'=> string'我们计算的请求签名与您提供的签名不匹配。检查您的AWS Secret Access Key和签名方法。有关详细信息,请参阅服务文档。 (长度= 178)
public ' RequestId'=> string'6e9cdea8-c0fc-46eb-aa61-a0df07a6a19f'(长度= 36)







我的代码



 <   pre  > 函数aws_signed_request($ region,
$ params,
$ public_key,
$ private_key,
$ associate_tag)
{

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


$ params [服务] =AWSECommerceService;
$ params [AWSAccessKeyId] = $ public_key;
$ params [AssociateTag] = $ associate_tag;

$ params [Timestamp] = gmdate(Y-m-d \TH:i:s\Z);

$ params [Version] =2009-03-31;

/ * params需要按密钥排序,因为Amazon在
结束时这样做,然后生成相同的哈希值。如果params
不按顺序排列,那么生成的哈希值将与
Amazon不同,因此验证过程失败。
* /
ksort($ params);

$ 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);

$ string_to_sign = $ method。\ n。$ host。\ n。$ uri。\ n。
$ canonicalized_query;

/ *使用HMAC,SHA256和base64编码计算签名* /
$ signature = base64_encode(hash_hmac(sha256,
$ string_to_sign,$ private_key,True) );

/ *编码请求的签名* /
$ signature = str_replace(%7E,〜,rawurlencode($ signature));

/ *创建请求* /
$ request =http://。$ host。$ uri。?。$ canonicalized_query。& Signature =。$ signature ;

/ *我更喜欢使用CURL * /
$ ch = curl_init();
curl_setopt($ ch,CURLOPT_URL,$ request);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ ch,CURLOPT_TIMEOUT,15);
curl_setopt($ ch,CURLOPT_SSL_VERIFYHOST,0);

$ xml_response = curl_exec($ ch);

if($ xml_response === False)
{
return False;
}
else
{
/ *解析XML并返回一个SimpleXML对象,如果你想
而不是raw xml那么只返回$ xml_response。
* /
$ parsed_xml = @simplexml_load_string($ xml_response);
return($ parsed_xml === False)?错误:$ parsed_xml;
}
}
?>





________________



 require_once '  aws_signed_request.php'; 

class AmazonProductAPI
{

private $ public_key = AWSAccessKeyId;
private $ private_key = mohamed< /跨度>;

/ * 现在需要'Associate Tag',自2011年10月25日起生效* /
private $ associate_tag = AWSSecretKey ;;




const MUSIC =
音乐 ;
const DVD =
DVD ;
const GAMES =
VideoGames ;

private function verifyXmlResponse($ response )
{
if($ response === False)
{
抛出新的异常(
无法连接到Amazon );
}
else
{
if(isset($ response-> Items-> ;本期特价货品> ItemAttributes-> ;标题))
{
return($ response);
}
else
{
抛出新的异常(
无效的xml响应。 );
}
}
}
< pre>公共功能searchProducts($ search,$ category,$ searchType =
UPC
{
$ allowedTypes = array(
UPC TITLE ARTIST KEYWORD < span class =code-string>);
$ allowedCategories = array(
音乐 DVD VideoGames );

switch($ searchType)
{
case
UPC
$ parameters = array(
操作 => ItemLookup
ItemId => $ search,
SearchIndex => $ category,
IdType => UPC
ResponseGroup => );
休息;

case
TITLE
$ parameters = array(
操作 => ItemSearch
标题 => $ search,
SearchIndex => $ category,
ResponseGroup => );
休息;

}

$ xml_response = $ this-> queryAmazon($ parameters);

返回$ this-> verifyXmlResponse($ xml_response);

}
}
< / pre>

解决方案

区域,

参数,可以

PUBLIC_KEY,

hi2all
i got this error when i try to use itemlookup in amzone api

public 'Code' => string 'SignatureDoesNotMatch' (length=21)
     public 'Message' => string '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.' (length=178)
 public 'RequestId' => string '6e9cdea8-c0fc-46eb-aa61-a0df07a6a19f' (length=36)




my code

<pre>function  aws_signed_request($region,
                             $params,
                             $public_key,
                             $private_key,
                             $associate_tag)
{
 
    $method = "GET";
    $host = "ecs.amazonaws.".$region;
    $uri = "/onca/xml";
 
 
    $params["Service"]          = "AWSECommerceService";
    $params["AWSAccessKeyId"]   = $public_key;
    $params["AssociateTag"]     = $associate_tag;
 
    $params["Timestamp"]        = gmdate("Y-m-d\TH:i:s\Z");
    
    $params["Version"]          = "2009-03-31";
 
    /* The params need to be sorted by the key, as Amazon does this at
      their end and then generates the hash of the same. If the params
      are not in order then the generated hash will be different from
      Amazon thus failing the authentication process.
    */
    ksort($params);
 
    $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);
 
    $string_to_sign = $method."\n".$host."\n".$uri."\n".
                            $canonicalized_query;
 
    /* calculate the signature using HMAC, 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));
 
    /* create request */
    $request = "http://".$host.$uri."?".$canonicalized_query."&Signature=".$signature;
 
    /* I prefer using CURL */
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$request);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 15);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
 
    $xml_response = curl_exec($ch);
 
    if ($xml_response === False)
    {
        return False;
    }
    else
    {
        /* parse XML and return a SimpleXML object, if you would
           rather like raw xml then just return the $xml_response.
         */
        $parsed_xml = @simplexml_load_string($xml_response);
        return ($parsed_xml === False) ? False : $parsed_xml;
    }
}
?>



________________

require_once 'aws_signed_request.php';

class AmazonProductAPI
{

    private $public_key     = "AWSAccessKeyId";
    private $private_key    = "mohamed";

    /* 'Associate Tag' now required, effective from 25th Oct. 2011 */
    private $associate_tag  = "AWSSecretKey;;




    const MUSIC = "Music";
    const DVD   = "DVD";
    const GAMES = "VideoGames";

    private function verifyXmlResponse($response)
    {
        if ($response === False)
        {
            throw new Exception("Could not connect to Amazon");
        }
        else
        {
            if (isset($response->Items->Item->ItemAttributes->Title))
            {
                return ($response);
            }
            else
            {
                throw new Exception("Invalid xml response.");
            }
        }
    }
<pre>public function searchProducts($search,$category,$searchType="UPC")
    {
        $allowedTypes = array("UPC", "TITLE", "ARTIST", "KEYWORD");
        $allowedCategories = array("Music", "DVD", "VideoGames");
 
        switch($searchType) 
        {
            case "UPC" :
                $parameters = array("Operation"     => "ItemLookup",
                                    "ItemId"        => $search,
                                    "SearchIndex"   => $category,
                                    "IdType"        => "UPC",
                                    "ResponseGroup" => "Medium");
                            break;
 
            case "TITLE" :
                $parameters = array("Operation"     => "ItemSearch",
                                    "Title"         => $search,
                                    "SearchIndex"   => $category,
                                    "ResponseGroup" => "Medium");
                            break;
 
        }
 
        $xml_response = $this->queryAmazon($parameters);
     
        return $this->verifyXmlResponse($xml_response);
 
    }
}
</pre>

解决方案

region,


params,


public_key,


这篇关于Amazone API SignatureDoesNotMatch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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