【发布时间】:2018-01-30 16:44:44
【问题描述】:
import re
进口时间 导入 urllib 导入base64 导入 hmac 导入哈希库 导入请求 将 xml.etree.ElementTree 导入为 ET 从 xml.etree 导入 ElementTree 作为 et 导入编解码器 将 xml.etree.cElementTree 导入为 EF 从日期时间导入日期
导入 bitly_api
def aws_signed_request(region, params, public_key, private_key, associate_tag=None, version='2011-08-01'):
# some paramters
method = 'GET'
host = 'webservices.amazon.' + region
uri = '/onca/xml'
# additional parameters
params['Service'] = 'AWSECommerceService'
params['AWSAccessKeyId'] = public_key
params['Timestamp'] = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime())
params['Version'] = version
if associate_tag:
params['AssociateTag'] = associate_tag
# create the canonicalized query
canonicalized_query = [urllib.quote(param).replace('%7E', '~') + '=' + urllib.quote(params[param]).replace('%7E', '~')
for param in sorted(params.keys())]
canonicalized_query = '&'.join(canonicalized_query)
# create the string to sign
string_to_sign = method + '\n' + host + '\n' + uri + '\n' + canonicalized_query
#print string_to_sign
# calculate HMAC with SHA256 and base64-encoding
signature = base64.b64encode(hmac.new(key=private_key, msg=string_to_sign, digestmod=hashlib.sha256).digest())
# encode the signature for the request
signature = urllib.quote(signature).replace('%7E', '~')
print 'http://' + host + uri + '?' + canonicalized_query + '&Signature=' + signature
return 'http://' + host + uri + '?' + canonicalized_query + '&Signature=' + signature
这是我以前可以正常工作的代码的一部分。它将获得几个类别中最畅销的亚马逊商品。但是,此脚本不再起作用。有人发现有什么不对吗?
【问题讨论】:
-
我在格式化帖子中的代码时遇到了一些问题。你可以在这里查看代码:ulrichmierendorff.com/files/aws_hmac_signer/…
标签: python amazon-web-services amazon