1

我写了以下代码:

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 参数添加到 sign_to 变量并请求变量,我会收到错误 SignatureDoesNotMatch。

请帮帮我。

4

2 回答 2

1

它实际上没有回答你的问题,但我建议你看看亚马逊产品广告 API 的优秀 python 包装器 - python-amazon-product-api

于 2012-09-04T20:29:46.833 回答
1

在文档中很难找到,但您必须确保您的操作列表按字母顺序排列,否则会出现SignatureDoesNotMatch错误。

例如,ItemPage必须介于AssociateTag和之间Keywords才有效。

AWSAccessKeyId
AssociateTag
ItemPage
Keywords
Operation
ResponseGroup
SearchIndex
Service
SignatureVersion
Timestamp
Version
于 2013-02-23T03:41:14.713 回答