【问题标题】:Amazon API signed requests isn't working properlyAmazon API 签名请求无法正常工作
【发布时间】:2010-10-23 16:10:53
【问题描述】:

代码如下:

import urllib2
import base64,hashlib,hmac,time
from urllib import urlencode
from xml.dom import minidom

AWS_ACCESS_KEY_ID = "secret"
AWS_SECRET_ACCESS_KEY = "secret"

base_url = "http://ecs.amazonaws.com/onca/xml"
url_params = {"Version": "2010-09-01",
              "Operation": "ItemSearch",
              "ResponseGroup": "Images",
              "SearchIndex": "Books",
              "Keywords": "Python",
              "AWSAccessKeyId": AWS_ACCESS_KEY_ID,
              "Service": "AWSCommerceService"
             }


# Add a ISO 8601 compliant timestamp (in GMT)
url_params['Timestamp'] = time.strftime("%Y-%m-%dT%H:%M:%S.000Z", time.gmtime())

# Sort the URL parameters by key
keys = url_params.keys()
keys.sort()
# Get the values in the same order of the sorted keys
values = map(url_params.get, keys)

# Reconstruct the URL paramters and encode them
url_string = urlencode(zip(keys,values))
url_string = url_string.replace('+',"%20")
url_string = url_string.replace(':',"%3A")

#Construct the string to sign
string_to_sign = """GET
ecs.amazonaws.com
/onca/xml
%s""" % url_string

# Sign the request
signature = hmac.new(
            key=AWS_SECRET_ACCESS_KEY,
            msg=string_to_sign,
            digestmod=hashlib.sha256).digest()

# Base64 encode the signature
signature = base64.encodestring(signature)

# Make the signature URL safe
signature = signature.replace('+','%20')
signature = signature.replace('=','%3D')
signature = signature.replace(':','%3A')
url_string += "&Signature=%s" % signature

print "%s?%s" % (base_url,url_string)

与 Django 一起使用时,我经常收到 403 Forbidden 错误。在没有 Django 的情况下使用时,它有时有效,有时无效。我不知道这可能是什么原因。

【问题讨论】:

  • 是否支持产品广告 API?

标签: python django amazon-web-services


【解决方案1】:

Adam Cox 有 posted a nice example 用于签署请求 URL: 找不到页面

当然,你可以偷懒使用python-amazon-product-api

如果您需要在 Google App Engine 上使用它,请查看此线程: Using python-amazon-product-api on Google Appengine without lxml

【讨论】:

    猜你喜欢
    • 2023-03-14
    • 2014-11-03
    • 2014-04-21
    • 1970-01-01
    • 2018-07-13
    • 2017-04-30
    • 2015-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多