请求签名与使用Python为Amazon AWS提供的签名不匹配
所以我试图从他们的API中收集来自亚马逊的评论。 不幸的是,尽管我的程序在某些时候可能会做错某些事情。 它发回了许多其他人显然获得的回应。 相信我,我已经经历过,看起来,每个人都隐藏问题,没有任何工作。 请帮帮我。
代码如下:
__author__ = 'dperkins'
import requests
import amazonproduct
import time
import datetime
import hmac
import hashlib
import base64
import urllib
import ssl
from bs4 import BeautifulSoup
# Configuration for the AWS credentials
config = {
'access_key': 'XXXXXXXXXXXXXXXXXXXX',
'secret_key': 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
'associate_tag': 'dperkgithu-20',
'locale': 'us'
}
api = amazonproduct.API(cfg=config)
productASIN = ''
productTitle = ''
# Product look up for the official iPhone 5s White 16gb Unlocked
for product in api.item_search('Electronics', Keywords='iPhone'):
if product .ASIN == 'B00F3J4E5U':
productTitle = product.ItemAttributes.Title
productASIN = product.ASIN
# Product Title with ASIN and a formatted underline
print productTitle + ': ' + productASIN
underline = ''
for int in range(len(productTitle + ': ' + productASIN)):
underline += '-'
print underline
# URL First portion of the request for reviews
signatureTime = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
signatureTime = urllib.quote_plus(signatureTime) # Must url encode the timestamp
url = "http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&Operation=ItemLookup&ResponseGroup=Reviews&IdType=ASIN&ItemId=%s&AssociateTag=%s&AWSAccessKeyId=%s&Timestamp=%s" % (productASIN, api.associate_tag, api.access_key, signatureTime)
# # HMAC with SHA256 hash algorithm
# dig = hmac.new(api.secret_key, msg=url, digestmod=hashlib.sha256).digest()
# signature = base64.b64encode(dig).decode() # py3k-mode
#url = 'http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&Operation=ItemLookup&ResponseGroup=Reviews&IdType=ASIN&ItemId=%s&AssociateTag=%s&AWSAccessKeyId=%s&Timestamp=%s&Signature=%s' % (productASIN, api.associate_tag, api.access_key, signatureTime, signature)
#Split and byte order the request (poorly but should always be the same)
parameters = [1, 2, 3, 4, 5, 6, 7]
for line in url.split('&'):
if (line.startswith('AssociateTag')):
parameters[0] = line
elif (line.startswith('AWSAccessKeyId')):
parameters[1] = line
elif (line.startswith('IdType')):
parameters[2] = line
elif (line.startswith('ItemId')):
parameters[3] = line
elif (line.startswith('Operation')):
parameters[4] = line
elif (line.startswith('ResponseGroup')):
parameters[5] = line
elif (line.startswith('Timestamp')):
parameters[6] = line
rejoined = ''
i = 1
for line in parameters:
if i < len(parameters):
rejoined += line + '&'
else:
rejoined += line
i += 1
print 'Rejoined: ' + rejoined
# Prepend the request beginning
prepend = 'GETnwebservices.amazon.comn/onca/xmln' + rejoined
print 'Prepend: ' + prepend
# HMAC with SHA256 hash algorithm
dig = hmac.new(api.access_key, msg=prepend, digestmod=hashlib.sha256).digest()
signature = base64.b64encode(dig).decode() # py3k-mode
print 'Signature: ' + signature
encodedSignature = urllib.quote_plus(signature) # encode the signature
print 'Encoded Signature: ' + encodedSignature
finalRequest = 'http://webservices.amazon.com/onca/xml?' + rejoined + '&Signature=' + encodedSignature
# Final request to send
print 'URL: ' + finalRequest
# Use BeautifulSoup to create the html
r = requests.get(finalRequest)
soup = BeautifulSoup(r.content)
print soup.prettify()
这是回应:
Apple iPhone 5s, Gold 16GB (Unlocked): B00F3J4E5U
-------------------------------------------------
Rejoined: AssociateTag=dperkgithu-20&AWSAccessKeyId=XXXXXXXXXXXXXXXXXXXX&IdType=ASIN&ItemId=B00F3J4E5U&Operation=ItemLookup&ResponseGroup=Reviews&Timestamp=2014-10-01T19%3A36%3A41Z
Prepend: GET
webservices.amazon.com
/onca/xml
AssociateTag=dperkgithu-20&AWSAccessKeyId=XXXXXXXXXXXXXXXXXXXX&IdType=ASIN&ItemId=B00F3J4E5U&Operation=ItemLookup&ResponseGroup=Reviews&Timestamp=2014-10-01T19%3A36%3A41Z
Signature: YAeIaDuigxbTX7AoZzRreZzn//RbIucCiwsG9VqMayQ=
Encoded Signature: YAeIaDuigxbTX7AoZzRreZzn%2F%2FRbIucCiwsG9VqMayQ%3D
URL: http://webservices.amazon.com/onca/xml?AssociateTag=dperkgithu-20&AWSAccessKeyId=XXXXXXXXXXXXXXXXXXXX&IdType=ASIN&ItemId=B00F3J4E5U&Operation=ItemLookup&ResponseGroup=Reviews&Timestamp=2014-10-01T19%3A36%3A41Z&Signature=YAeIaDuigxbTX7AoZzRreZzn%2F%2FRbIucCiwsG9VqMayQ%3D
<html>
<body>
<itemlookuperrorresponse xmlns="http://ecs.amazonaws.com/doc/2005-10-05/">
<error>
<code>
SignatureDoesNotMatch
</code>
<message>
The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.
</message>
</error>
<requestid>
c159b688-9b08-4cc9-94fe-35245aa69cc9
</requestid>
</itemlookuperrorresponse>
</body>
</html>
您已经有成功的亚马逊产品广告API请求。 如果您想查看返回的XML,请使用产品对象。
至于你的评论,他们不再以纯文本的形式通过API提供。 您需要直接从Amazon.com上刮取HTML。
链接地址: http://www.djcxy.com/p/39025.html上一篇: Request signature does not match signature provided for Amazon AWS using Python