laclefblog logo

Sun, 10 May 2009

AmazonのProduct Advertising API

というわけで、Pyblosxomの Amazonリンク生成プラグイン に変更の必要を迫られたので、準備。

import urllib
import hmac
import hashlib
import base64

key = '1234567890'  # AWSのページでSecret Access Keyをチェック
aws_access_key = '00000000000000000000'  # Amazon Access Key
isbn = '0679722769'  # ItemId
request_url = 'webservices.amazon.co.jp'

option_dict = {
    'Service': 'AWSECommerceService',
    'AWSAccessKeyId': aws_access_key,
    'Operation': 'ItemLookup',
    'ItemId': isbn,
    'ResponseGroup': 'ItemAttributes,Offers,Images,Reviews',
    'Version': '2009-01-06',
    'Timestamp': '2009-01-01T12:00:00Z',
}
option_seq =  [(k, option_dict[k]) for k in sorted(option_dict.keys())]
option_list = urllib.urlencode(option_seq)
message = "\n".join(["GET", request_url, "/onca/xml", option_list])
print message
message_hmac = hmac.new(key, message, hashlib.sha256)
signature = base64.b64encode(message_hmac.digest())
print signature

request_urlを'webservices.amazon.com'にしてサンプルと結果が一致することを確認してから、'webservices.amazon.co.jp'に変更した。

11日以降に、リクエストが通ることを確認して、プラグインを書き換える。