简体   繁体   English

亚马逊产品广告API签名

[英]Amazon Product Advertising API Signature

I am trying to produce a signature for the Amazon Product Advertising API, been at it a few hours and am still getting a 403 - could anyone have a quick look at the code and tell me if I am doing anything wrong please? 我正在尝试为亚马逊产品广告API制作一个签名,已经花了几个小时,我仍然得到一个403 - 任何人都可以快速查看代码并告诉我,如果我做错了什么吗?

This is the function I use to create the signature 这是我用来创建签名的功能

def create_signature(service, operation, version, search_index, keywords, associate_tag, time_stamp, access_key):
start_string = "GET\n" + \
               "webservices.amazon.com\n" + \
               "/onca/xml\n" + \
               "AWSAccessKeyId=" + access_key + \
               "&AssociateTag=" + associate_tag + \
               "&Keywords=" + keywords + \
               "&Operation=" + operation + \
               "&SearchIndex=" + search_index + \
               "&Service=" + service + \
               "&Timestamp=" + time_stamp + \
               "&Version=" + version

dig = hmac.new("MYSECRETID", msg=start_string, digestmod=hashlib.sha256).digest()
sig = urllib.quote_plus(base64.b64encode(dig).decode())

return sig;

And this is the function I use to return the string for the request 这是我用来返回请求字符串的函数

def ProcessRequest(request_item):
    start_string = "http://webservices.amazon.com/onca/xml?" + \
                   "AWSAccessKeyId=" + request_item.access_key + \
                   "&AssociateTag=" + request_item.associate_tag + \
                   "&Keywords=" + request_item.keywords + \
                   "&Operation=" + request_item.operation + \
                   "&SearchIndex=" + request_item.search_index + \
                   "&Service=" + request_item.service + \
                   "&Timestamp=" + request_item.time_stamp + \
                   "&Version=" + request_item.version + \
                   "&Signature=" + request_item.signature
    return start_string;

And this is the run code 这是运行代码

_AWSAccessKeyID = "MY KEY"
_AWSSecretKey= "MY SECRET KEY"

def ProduceTimeStamp():
    time = datetime.datetime.now().isoformat()
    return time;

item = Class_Request.setup_request("AWSECommerceService", "ItemSearch", "2011-08-01", "Books", "harry%20potter", "PutYourAssociateTagHere", ProduceTimeStamp(), _AWSAccessKeyID)
item2 = Class_Request.ProcessRequest(item)

An example web request it spits out that produces at 403 is this:- 它在403处产生的示例Web请求是: -

http://webservices.amazon.com/onca/xml?AWSAccessKeyId=AKIAIY4QS5QNDAI2NFLA&AssociateTag=PutYourAssociateTagHere&Keywords=harry%20potter&Operation=ItemSearch&SearchIndex=Books&Service=AWSECommerceService&Timestamp=2015-02-26T23:53:14.330000&Version=2011-08-01&Signature=KpC%2BUsyJcw563LzIgxf7GkYI5IV6EfmC0%2FsH8LuP%2FEk%3D

There is also a holder class called ClassRequest that just has a field for every request field 还有一个名为ClassRequest的holder类,它只为每个请求字段都有一个字段

The instructions I followed are here if anyone is intrested:- http://docs.aws.amazon.com/AWSECommerceService/latest/DG/rest-signature.html 如果有人有兴趣,我所遵循的指示就在这里: - http://docs.aws.amazon.com/AWSECommerceService/latest/DG/rest-signature.html

I hope someone can help, I am new to Python and a bit lost 我希望有人可以提供帮助,我是Python新手,有点迷失

You can simply use one of the existing solutions 您只需使用一种现有解决方案即可

available from PyPI . 可从PyPI获得

OR 要么

Compare your solution to one of those: https://bitbucket.org/basti/python-amazon-product-api/src/41529579819c75ff4f03bc93ea4f35137716ebf2/amazonproduct/api.py?at=default#cl-143 将您的解决方案与其中一个进行比较: https//bitbucket.org/basti/python-amazon-product-api/src/41529579819c75ff4f03bc93ea4f35137716ebf2/amazonproduct/api.py?at=default#cl-143

Your timestamp, for instance, looks a bit short. 例如,您的时间戳看起来有点短。

Check again that the timestamp is right, it should have the format of 2015-03-27T15:10:17.000Z and in your example web request it looks like: 2015-02-26T23:53:14.330000 再次检查时间戳是否正确,它应该具有2015-03-27T15:10:17.000Z的格式,并且在您的示例Web请求中看起来像:2015-02-26T23:53:14.330000

A good tool to try out your links is Amazon's signed requests helper: https://associates-amazon.s3.amazonaws.com/signed-requests/helper/index.html 尝试链接的好工具是亚马逊签署的请求帮助程序: https//associates-amazon.s3.amazonaws.com/signed-requests/helper/index.html

That Worked for me. 这对我有用。

$str = "Service=AWSECommerceService&Operation=ItemSearch&AWSAccessKeyId={Access Key}&Keywords=Harry%20Potter&ResponseGroup=Images%2CItemAttributes%2COffers&SearchIndex=Books&Timestamp=2019-08-11T17%3A51%3A56.000Z";


$ar = explode("&", $str);

natsort($ar);

$str = "GET
webservices.amazon.com
/onca/xml
";

$str .= implode("&", $ar); 

$str = urlencode(base64_encode(hash_hmac("sha256",$str,'{Secret Key Here}',true)));


http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&Operation=ItemSearch&AWSAccessKeyId={Access Key}&Keywords=Harry%20Potter&ResponseGroup=Images%2CItemAttributes%2COffers&SearchIndex=Books&Timestamp=2019-08-11T17%3A51%3A56.000Z&Signature=$str

Remember: If you get this error Your AccessKey Id is not registered for Product Advertising API. 请记住:如果您收到此错误,您的AccessKey ID未在Product Advertising API中注册。 Please use the AccessKey Id obtained after registering at https://affiliate-program.amazon.com/assoc_credentials/home 请使用https://affiliate-program.amazon.com/assoc_credentials/home上注册后获得的AccessKey ID

Go to https://affiliate-program.amazon.com/assoc_credentials/home 转到https://affiliate-program.amazon.com/assoc_credentials/home

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM