昨日のエントリーと同じになってしまうが、後で、検索しやすいようにタイトルを変えて書いてみた。
下のコードは、google app engineのpythonを使ってamazon product advertising apiのBrowseNodeLookupをコールする処理です。
#!/usr/bin/env python # # Copyright 2007 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # from urlparse import urlparse import urllib import datetime import base64 import hmac import hashlib amazon_api_access_key_id = "ここにはaccess_key_id" amazon_api_secret_access_key = "ここにはsecret_access_key" amazon_api_associatetag = "ここにはassociatetag" amazon_api_product_advertising_request_url = "http://webservices.amazon.co.jp/onca/xml" amazon_api_service = "AWSECommerceService" amazon_api_browsenodelookup = "BrowseNodeLookup" amazon_api_browsenode_book = "ここにはbrowse node" def urlencode_rfc3986(str): s = urllib.quote(str) s = s.replace("%7E", "~") return s def create_canonical_string(d): keys = sorted(d) canonical_string = "" for k in keys: canonical_string = canonical_string + "&" + urlencode_rfc3986(k) + "=" + urlencode_rfc3986(d[k]) else: canonical_string = canonical_string[1:] return canonical_string def create_request_to_amazon_url(d): canonical_string = create_canonical_string(d) parsed_url = urlparse(amazon_api_product_advertising_request_url) string_to_sign = "GET\n" + parsed_url.hostname + "\n" + parsed_url.path +"\n" + canonical_string signature = hmac.new(amazon_api_secret_access_key, string_to_sign, hashlib.sha256).digest() signature = base64.b64encode(signature) url = amazon_api_product_advertising_request_url + "?" + canonical_string + "&Signature=" + urlencode_rfc3986(signature) return url呼び出しは下のような形でコール
#!/usr/bin/env python # # Copyright 2007 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # import webapp2 import jinja2 import constants import datetime from google.appengine.api import urlfetch from xml.etree import ElementTree class BrowseNodeLookupHandler(webapp2.RequestHandler): def get(self): self.response.headers['Content-Type'] = 'text/html' params = {} params["Service"] = constants.amazon_api_service params["AWSAccessKeyId"] = constants.amazon_api_access_key_id params["AssociateTag"] = constants.amazon_api_associatetag params["Operation"] = constants.amazon_api_browsenodelookup params["Timestamp"] = datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ") params["BrowseNodeId"] = constants.amazon_api_browsenode_apparel url = constants.create_request_to_amazon_url(params) result = urlfetch.fetch(url) if result.status_code == 200: xmlTree = ElementTree.fromstring(result) app = webapp2.WSGIApplication([ ('/search/browsenodelookup', BrowseNodeLookupHandler) ], debug=True)BrowseNodeLookupは、ロケールによって値が変わるのでそこは注意をする必要がある。
0 コメント:
コメントを投稿