2014-10-01 40 views
0

因此,我試圖從使用其API的Amazon收集評論。不幸的是,儘管我的程序在某些時候可能會做錯某些事情。它發回了許多其他人顯然獲得的迴應。相信我,我已經經歷過,看起來,每個人都隱藏問題,沒有任何工作。請幫幫我。請求籤名與使用Python爲亞馬遜AWS提供的簽名不匹配

下面的代碼:

__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> 
+0

爲什麼你不使用boto這個? – 2014-10-02 13:21:50

+0

我不完全確定你可以提取產品信息(如評論),或者執行我試圖用boto做的REST請求。如果我錯了,請糾正我。 – 2014-10-03 13:29:38

+0

我對它並不是很熟悉,但它似乎支持很多AWS服務:https://aws.amazon.com/sdk-for-python/而且我知道它的確如此REST – 2014-10-03 16:15:43

回答

1

您已經有了一個成功的請求亞馬遜產品廣告API。如果您想查看返回的XML,請使用產品對象。

至於你的評論,它們不再以純文本的形式通過API提供。您需要直接從Amazon.com上刮取HTML。

+0

謝謝,yea帶我去了同時在我發佈這個消息後弄清楚,但我做得正確。完全按照你所說的只是解析。 – 2015-06-19 01:57:48

相關問題