2014-10-01 4 views
0

그래서 Amazon에서 API를 사용하여 리뷰를 수집하려고합니다. 불행히도 내 프로그램에서 어떤 점에서 잘못된 일을하고있는 것으로 보입니다. 그것은 많은 사람들이 분명히 얻고있는 응답을 되돌려 보내는 것입니다. 나를 믿어 라, 나는 끝까지 다녔다. 그리고 모두는 질문한다. 그리고 아무것도 작용하지 않고 있었다. 도와주세요. 여기에 응답이다요청 서명이 Python을 사용하는 Amazon 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를 사용하지 않습니까? –

+0

제품 정보 (예 : 리뷰)를 가져 오거나 내가 boto로하려는 REST 요청을 수행 할 수 있는지 잘 모르겠습니다. 내가 틀렸다면 나를 바로 잡으십시오. –

+0

나는 그것에 익숙하지 않지만 AWS 서비스를 많이 지원하는 것으로 보인다. https://aws.amazon.com/sdk-for-python/ 그리고 REST를 알 수있다. –

답변

1

는 이미 아마존 제품 광고 API에 성공적으로 요청을해야합니다. 리턴 된 XML을 들여다보고 싶다면 제품 오브젝트를 사용하십시오.

리뷰를 통해 더 이상 API를 통해 일반 텍스트로 사용할 수 없습니다. Amazon.com에서 HTML을 직접 가져와야합니다.

+0

감사합니다. 내가 이것을 게시 한 후에 그것을 알아내는 동안 나는 올바르게 그것을하고 있었다. 당신이 말한 것과 파싱 만하는 것을 끝내 었습니다. –

관련 문제