以下是代码:
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的情况下使用它有时会工作,有时则不会。我不知道这可能是什么原因。
答案 0 :(得分:2)
Adam Cox的签名请求网址为posted a nice example: 找不到网页
当然,你可以懒惰并使用python-amazon-product-api。
如果您需要在Google App Engine上使用它,请查看以下主题: Using python-amazon-product-api on Google Appengine without lxml