pyAAWS for Product Advertising API
Python library of Amazon Associates Web Service を Product Advertising API に対応させました。電子署名とか・・・。追加点は新API仕様のとおりです。解説がめんどいので、diff 貼ります。
+import hashlib +import hmac +import base64 -Version = '2009-02-01' +Version = '2009-03-31' def _get_url(self, kw): - url = 'http://%s/onca/xml?Service=%s&AWSAccessKeyId=%s&Version=%s&' % ( - supported_locales[self.locale], - Service, - self.AWSAccessKeyId, - Version, - ) if not kw: kw = {} + kw['Service'] = Service + kw['Version'] = Version + kw['AWSAccessKeyId'] = self.AccessKeyId + kw['Timestamp'] = datetime.datetime.utcnow().isoformat() if (not 'AssociateTag' in kw) or (not kw['AssociateTag'] and self.AssociateTag): kw['AssociateTag'] = self.AssociateTag if (not 'MerchantId' in kw) or (not kw['MerchantId'] and self.MerchantId): kw['MerchantId'] = self.MerchantId if (not 'Validate' in kw) or (not kw['Validate'] and Validate): kw['Validate'] = Validate - url += '&'.join(['%s=%s' % (k, urllib.quote(unicode(v))) \ - for (k, v) in kw.items() if v]) - return url + [kw.pop(k) for (k, v) in kw.items() if not v] + payload = urllib.urlencode(sorted(kw.items())) + urllib2.quote + strings = [ + 'GET', + supported_locales[self.locale], + end_point, + payload + ] + digest = hmac.new( + self.SecretAccessKey, + '\n'.join(strings), + hashlib.sha256 + ).digest() + signature = base64.b64encode(digest) + return 'http://%(uri)s%(end_point)s?%(payload)s&Signature=%(signature)s' % { + 'uri':supported_locales[self.locale], + 'end_point':end_point, + 'payload':payload, + 'signature':signature, + }
詳細の diff は bitbucket (なんちゅうブログやw) へ。ノットリファクタリング。使い方が変わったところは、Secret Access Key が必須になったところぐらいです。
import awws - api = Api('[取得した Access Key Id]') + api = Api('[取得した Access Key Id]', '[取得した Secret Access Key]')
コメント
コメントを投稿