AWS Product API ItemLookup Sample code in Python3

According to http://docs.aws.amazon.com/AWSECommerceService/latest/DG/rest-signature.html


import requests
import hmac
import hashlib
import base64

access_key = 'xxx' # https://console.aws.amazon.com/iam/home?rw_useCurrentProtocol=1#/security_credential
secret_key = 'xxx'

ts = datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')

params = {'Service': 'AWSECommerceService',
    'AWSAccessKeyId': access_key,
    'AssociateTag': 'asterisk37n-22',
    'Operation': 'ItemLookup',
    'ItemId': '0679722769',
    'ResponseGroup': 'Images,ItemAttributes,Offers,Reviews',
    'Version': '2013-08-01'}
parsed_url_params = urllib.parse.urlencode(sorted(params.items()))

params.update({'Timestamp': ts})

parsed_canonical_params = urllib.parse.urlencode(sorted(params.items()))
endpoint = 'http://webservices.amazon.co.jp/onca/xml?'

canonical_string = '''GET
webservices.amazon.co.jp
/onca/xml
''' + parsed_canonical_params

dig = hmac.new(secret_key.encode('utf-8'), msg=canonical_string.encode('utf-8'), digestmod=hashlib.sha256).digest()
code = base64.b64encode(dig).decode()      # py3k-mode
encoded_signature = urllib.parse.quote(code)

url = endpoint + parsed_url_params + '&Timestamp=' + urllib.parse.quote(ts) + '&Signature=' + encoded_signature
print(url)
requests.get(url).text

コメント

このブログの人気の投稿

株式会社GAIAの説明会で驚いた - パチンコホールの売上や給与 -

Python機械学習scikit-learn入門 SVMの学習とクロス・バリデーション (K-fold)