我写了以下代码:
from hashlib import sha256
from base64 import b64encode
import hmac
import urllib
from time import strftime, gmtime
url = 'http://ecs.amazonaws.com/onca/xml'
AWSAccessKeyId = amazon_settings.amazon_access_key_id
AssociateTag = amazon_settings.amazon_associate_tag
Keywords = urllib.quote_plus('Potter')
Operation = 'ItemSearch'
SearchIndex = 'Books'
Service = 'AWSECommerceService'
Timestamp = urllib.quote_plus(strftime("%Y-%m-%dT%H:%M:%S.000Z", gmtime()))
Version = '2011-08-01'
sign_to = 'GET\necs.amazonaws.com\n/onca/xml\nAWSAccessKeyId=%s&AssociateTag=%s&Keywords=%s&Operation=%s&SearchIndex=%s&Service=%s&Timestamp=%s&Version=%s' % (AWSAccessKeyId, AssociateTag, Keywords, Operation, SearchIndex, Service, Timestamp, Version)
Signature = urllib.quote_plus(b64encode(hmac.new(str(amazon_settings.amazon_secret_access_key), str(sign_to), sha256).digest()))
request = '%s?AWSAccessKeyId=%s&AssociateTag=%s&Keywords=%s&Operation=%s&SearchIndex=%s&Service=%s&Timestamp=%s&Version=%s&Signature=%s' % (url, AWSAccessKeyId, AssociateTag, Keywords, Operation, SearchIndex, Service, Timestamp, Version, Signature)
print request
当我使用此代码时,一切都很好。 但是如果我尝试将itemPage param添加到sign_to变量并请求变量我会收到错误SignatureDoesNotMatch。
请帮帮我。
答案 0 :(得分:1)
这实际上不是你的问题的答案,但我建议你看一下亚马逊产品广告API的优秀python包装器 - python-amazon-product-api
答案 1 :(得分:1)
在文档中很难找到,但您必须确保您的操作列表按字母顺序排列,否则会出现SignatureDoesNotMatch
错误。
例如,ItemPage
必须在AssociateTag
和Keywords
之间有效。
AWSAccessKeyId
AssociateTag
ItemPage
Keywords
Operation
ResponseGroup
SearchIndex
Service
SignatureVersion
Timestamp
Version