【问题标题】:Request signature does not match signature provided for Amazon AWS using Python请求签名与使用 Python 为 Amazon AWS 提供的签名不匹配
【发布时间】:2014-10-01 19:53:41
【问题描述】:

所以我正在尝试使用他们的 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 = 'GET\nwebservices.amazon.com\n/onca/xml\n' + 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>

【问题讨论】:

  • 你为什么不使用 boto 呢?
  • 我不完全确定您是否可以提取产品信息(例如评论)或执行我尝试使用 boto 执行的 REST 请求。如果我错了,请纠正我。
  • 我不是很熟悉,但它似乎支持很多AWS服务:aws.amazon.com/sdk-for-python而且我知道它支持REST

标签: python amazon python-requests amazon-product-api


【解决方案1】:

您已经成功请求亚马逊产品广告 API。如果您想查看返回的 XML,请使用产品对象。

至于您的评论,它们不再通过 API 以纯文本形式提供。您需要直接从 Amazon.com 抓取 HTML。

【讨论】:

  • 谢谢,是的,我在发布此内容后花了一段时间才弄清楚,但我做得正确。最终完全按照您所说的进行了解析。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-23
  • 2017-08-26
  • 2017-12-20
  • 2023-03-23
  • 2020-09-11
  • 1970-01-01
相关资源
最近更新 更多