【问题标题】:Amazon Product Advertising API Signature亚马逊产品广告 API 签名
【发布时间】:2015-02-26 23:55:18
【问题描述】:

我正在尝试为亚马逊产品广告 API 生成签名,已经使用了几个小时,但仍然收到 403 - 谁能快速查看代码并告诉我是否做错了什么?

这是我用来创建签名的函数

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;

这是我用来返回请求字符串的函数

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;

这是运行代码

_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)

它吐出的在 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

还有一个名为 ClassRequest 的持有者类,它只为每个请求字段提供一个字段

如果有人感兴趣,我遵循的说明在这里:- http://docs.aws.amazon.com/AWSECommerceService/latest/DG/rest-signature.html

希望有人能帮忙,我是 Python 新手,有点迷茫

【问题讨论】:

    标签: python amazon-product-api


    【解决方案1】:

    您可以简单地使用现有解决方案之一

    可从PyPI获得。

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

    例如,您的时间戳看起来有点短。

    【讨论】:

      【解决方案2】:

      再次检查时间戳是否正确,其格式应为 2015-03-27T15:10:17.000Z,在您的示例网络请求中,它看起来像:2015-02-26T23:53:14.330000

      亚马逊的签名请求助手是试用链接的好工具:https://associates-amazon.s3.amazonaws.com/signed-requests/helper/index.html

      【讨论】:

        【解决方案3】:

        这对我有用。

        $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
        

        记住:如果您收到此错误 您的 AccessKey Id 未注册产品广告 API。请使用https://affiliate-program.amazon.com/assoc_credentials/home注册后获得的AccessKey Id

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-06-12
          • 2012-05-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-10-29
          相关资源
          最近更新 更多