{"id":1288,"date":"2009-10-02T16:28:45","date_gmt":"2009-10-02T07:28:45","guid":{"rendered":"http:\/\/blog.bitmeister.jp\/?p=1288"},"modified":"2009-10-02T18:48:49","modified_gmt":"2009-10-02T09:48:49","slug":"amazon-api-perl%e3%81%a7%e7%bd%b2%e5%90%8d%e4%bb%98uri%e7%94%9f%e6%88%90-python%e3%81%a7xml%e3%82%92%e5%8f%96%e5%be%97","status":"publish","type":"post","link":"https:\/\/blog.bitmeister.jp\/?p=1288","title":{"rendered":"Amazon API -Perl\u3067\u7f72\u540d\u4ed8URI\u751f\u6210 Python\u3067XML\u3092\u53d6\u5f97-"},"content":{"rendered":"<p>Amazon Product Advertising API \u3078\u306e\u30ea\u30af\u30a8\u30b9\u30c8URI\u306e\u30d5\u30a9\u30fc\u30de\u30c3\u30c8\u304c\u5909\u66f4\u3055\u308c\u307e\u3057\u305f\u3002<br \/>\n\u30ea\u30af\u30a8\u30b9\u30c8URI\u306b\u7f72\u540d\u306e\u4ed8\u52a0\u304c\u5fc5\u9808\u3068\u306a\u308a\u3001\u30ea\u30af\u30a8\u30b9\u30c8\u5074\u304c\u7f72\u540d\u3092\u751f\u6210\u3057\u306a\u304f\u3066\u306f\u3044\u3051\u307e\u305b\u3093\u3002<br \/>\n ex. http:\/\/xml-jp.amznxslt.com\/onca\/xml?&#8230;&#038;Signature=*****<\/p>\n<p>\u8a73\u7d30\u306f<a href=\"http:\/\/affiliate.amazon.co.jp\/gp\/associates\/help\/t126\/a1?pf_rd_m=AN1VRQENFRJN5&#038;pf_rd_s=center-1&#038;pf_rd_r=&#038;pf_rd_t=501&#038;pf_rd_p=&#038;pf_rd_i=assoc_help_t126\" target=\"_blank\">\u30aa\u30d5\u30a3\u30b7\u30e3\u30eb\u30b5\u30a4\u30c8<\/a>\u306b\u8f09\u3063\u3066\u3044\u307e\u3059\u304c\u3001\u8981\u306f\u4e0b\u8a18\u306e2\u3064\u306e\u6587\u5b57\u5217\u3092 <a href=\"http:\/\/ja.wikipedia.org\/wiki\/HMAC\" target=\"_blank\">HMAC<\/a> <a href=\"http:\/\/ja.wikipedia.org\/wiki\/SHA\" target=\"_blank\">SHA256<\/a> \u3068\u3044\u3046\u65b9\u5f0f\u3067\u7b26\u53f7\u5316\u3057\u305f\u3082\u306e\u304c\u7f72\u540d\u3067\u3059\u3002<br \/>\n\u3000\u30fb&#8221;URI\u306e\u30af\u30a8\u30ea(\u30bf\u30a4\u30e0\u30b9\u30bf\u30f3\u30d7\u3092\u542b\u3080)&#8221;<br \/>\n\u3000\u30fb&#8221;\u30a2\u30de\u30be\u30f3\u304b\u3089\u767a\u884c\u3055\u308c\u308b\u79d8\u5bc6\u30ad\u30fc&#8221; <\/p>\n<p>\u3068\u3053\u308d\u304c\u3001\u3053\u308c\u307e\u3067\u30ea\u30af\u30a8\u30b9\u30c8URI\u751f\u6210\u306b\u4f7f\u3063\u3066\u3044\u305f\u30db\u30b9\u30c6\u30a3\u30f3\u30b0\u30b5\u30fc\u30d0\u306ePython\u306f\u30d0\u30fc\u30b8\u30e7\u30f3\u304c\u53e4\u304f(\u65e9\u304f\u30d0\u30fc\u30b8\u30e7\u30f3\u30a2\u30c3\u30d7\u3057\u3066\u307b\u3057\u3044\u2026)\u3001 HMAC SHA256 \u7b26\u53f7\u5316\u304c\u3067\u304d\u307e\u305b\u3093\u3002<br \/>\nPython 2.4 \u4ee5\u524d\u306f\u6a19\u6e96\u3067 <a href=\"http:\/\/ja.wikipedia.org\/wiki\/SHA\" target=\"_blank\">SHA256<\/a> \u3092\u7b97\u51fa\u3059\u308b\u30e2\u30b8\u30e5\u30fc\u30eb\u304c\u5165\u3063\u3066\u3044\u306a\u3044\u305f\u3081\u3067\u3059\u3002<br \/>\n\u203bPython 2.5 \u4ee5\u964d\u3067\u3042\u308c\u3070 <a href=\"http:\/\/www.python.jp\/doc\/2.5\/lib\/module-hashlib.html\"\u3000 target=\"_blank\">hashlib<\/a> \u30e2\u30b8\u30e5\u30fc\u30eb\u304c\u4f7f\u3048\u307e\u3059\u3002<br \/>\n\u4ed5\u65b9\u304c\u306a\u3044\u306e\u3067\u3001\u7f72\u540d\u4ed8\u304d\u30ea\u30af\u30a8\u30b9\u30c8URI\u3092\u751f\u6210\u3059\u308b\u30b9\u30af\u30ea\u30d7\u30c8\u306fPerl\u3067\u66f8\u304d\u3001\u305d\u308c\u3092Python\u304b\u3089\u5b9f\u884c\u3057\u3001URI\u3092\u8aad\u307f\u8fbc\u3080\u3053\u3068\u306b\u3057\u307e\u3057\u305f\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u4ee5\u4e0b\u3001Perl\u306eURI\u751f\u6210\u30b9\u30af\u30ea\u30d7\u30c8\u3002<br \/>\nAmazonSignedRequestGenerator.pl:<br \/>\n<code><\/p>\n<pre class=\"brush: perl; title: ; notranslate\" title=\"\">\r\n#!\/usr\/bin\/perl\r\nuse strict;\r\nuse warnings;\r\nuse URI::Escape;\r\nuse Digest::SHA qw(hmac_sha256_base64);\r\n\r\n#ISBN\u3092\u53d7\u3051\u53d6\u308a\u3001Amazon API\u306e\u30ea\u30af\u30a8\u30b9\u30c8URI\u3092\u8fd4\u3059\u95a2\u6570\r\nsub GetSignedURIByISBN{\r\n\r\n    # Amazon Access Key ID\r\n    my $accessKeyID = &quot;Your AWS Access Key ID&quot;;\r\n    # Amazon\u304b\u3089\u5272\u308a\u632f\u3089\u308c\u308b\u79d8\u5bc6\u30ad\u30fc\r\n    my $secretKey   = &quot;Your Secret Key&quot;;\r\n\r\n    # \u30ea\u30af\u30a8\u30b9\u30c8\u5148\u306e\u30db\u30b9\u30c8\u540d\r\n    my $host = &quot;xml-jp.amznxslt.com&quot;;\r\n    my $path = &quot;\/onca\/xml&quot;;\r\n\r\n    # \u30ea\u30af\u30a8\u30b9\u30c8\u30ad\u30fc\u3068\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u95a2\u9023\u4ed8\u3051\u305f\u9023\u60f3\u914d\u5217\u3092\u7528\u610f\u3059\u308b\r\n    my %reqParam = (\r\n        &quot;Service&quot;        =&gt; &quot;AWSECommerceService&quot;,\r\n        &quot;AWSAccessKeyId&quot; =&gt; $accessKeyID,\r\n        &quot;Version&quot;        =&gt; &quot;2007-10-29&quot;,\r\n        &quot;Operation&quot;      =&gt; &quot;ItemLookup&quot;,\r\n        &quot;IdType&quot;         =&gt; &quot;ISBN&quot;,\r\n        &quot;SearchIndex&quot;    =&gt; &quot;Books&quot;,\r\n        &quot;ItemId&quot;         =&gt; $_&#x5B;0], # \u5f15\u6570\u3067\u53d7\u3051\u53d6\u3063\u305fISBN\u304c\u5165\u308b\r\n        &quot;ResponseGroup&quot;  =&gt; &quot;Medium&quot;,\r\n        &quot;ContentType&quot;    =&gt; &quot;text\/xml&quot;,\r\n        &quot;Timestamp&quot;      =&gt; &amp;GetTimeStamp()\r\n    );\r\n\r\n    # \u30ea\u30af\u30a8\u30b9\u30c8\u30ad\u30fc\u3068\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u7d50\u5408\u3057\u305f\u30af\u30a8\u30ea\u3092\u4f5c\u6210\r\n    my @queryList = ();\r\n    foreach my $reqKey(sort(keys%reqParam)){ #\u30a2\u30eb\u30d5\u30a1\u30d9\u30c3\u30c8\u9806\u306b\u30bd\u30fc\u30c8\r\n        push(@queryList, URI::Escape::uri_escape($reqKey, &quot;^A-Za-z0-9\\-_.~&quot;).&quot;=&quot;.\r\n                         URI::Escape::uri_escape($reqParam{$reqKey}, &quot;^A-Za-z0-9\\-_.~&quot;));\r\n    }\r\n    my $query = join(&quot;&amp;&quot;, @queryList);\r\n\r\n    # \u7f72\u540d\u3092\u4f5c\u6210\r\n    my $signature = hmac_sha256_base64(&quot;GET\\n$host\\n$path\\n$query&quot;, $secretKey);\r\n    $signature .= &quot;=&quot; while length($signature) % 4; # \u7f72\u540d\u306e\u9577\u3055\u304c4\u306e\u500d\u6570\u306b\u306a\u308b\u307e\u3067&quot;=&quot;\u3092\u4ed8\u52a0\r\n    $signature = URI::Escape::uri_escape($signature, &quot;^A-Za-z0-9\\-_.~&quot;);\r\n\r\n    # \u30ea\u30af\u30a8\u30b9\u30c8URI\u3092\u8fd4\u3059\r\n    return &quot;http:\/\/$host$path?$query&amp;Signature=$signature&quot;;\r\n}\r\n\r\n# \u547c\u3070\u308c\u305f\u6642\u523b\u306e\u30bf\u30a4\u30e0\u30b9\u30bf\u30f3\u30d7\u3092\u8fd4\u3059\u95a2\u6570\r\nsub GetTimeStamp {\r\n    my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = gmtime();\r\n    return sprintf(&quot;%04d-%02d-%02dT%02d:%02d:%02dZ&quot;,\r\n                   $year+1900, $mon+1, $mday, $hour, $min, $sec);\r\n}\r\n\r\n# \u30b3\u30de\u30f3\u30c9\u5f15\u6570\u304b\u3089ISBN\u3092\u53d7\u3051\u53d6\u308a\u3001\u30ea\u30af\u30a8\u30b9\u30c8URI\u3092\u8868\u793a\r\nprint &amp;GetSignedURIByISBN($ARGV&#x5B;0]);\r\n<\/pre>\n<p><\/code><br \/>\n\u3000<br \/>\nPerl\u306eURI\u751f\u6210\u30b9\u30af\u30ea\u30d7\u30c8\u5b9f\u884c\u4f8b\u3002<br \/>\n\u5f15\u6570\u306bISBN\u3092\u6307\u5b9a\u3059\u308b\u3068\u3001\u305d\u306e\u66f8\u7c4d\u60c5\u5831XML\u306e\u30ea\u30af\u30a8\u30b9\u30c8URI\u3092\u51fa\u529b\u3059\u308b\u3002<br \/>\n<code><\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n$ perl AmazonSignedRequestGenerator.pl 4091400019\r\nhttp:\/\/xml-jp.amznxslt.com\/onca\/xml?AWSAccessKeyId=&quot;Your AWS Access Key ID&quot;&amp;ContentType=text%2Fxml&amp;IdType=ISBN&amp;ItemId=4091400019&amp;Operation=ItemLookup&amp;ResponseGroup=Medium&amp;SearchIndex=Books&amp;Service=AWSECommerceService&amp;Timestamp=2009-10-02T04%3A34%3A01Z&amp;Version=2007-10-29&amp;Signature=V3SNWy7FwOUQJ%2F9TiNJLK55UMdNEWuw5qmqd%2FRsY6Y4%3D\r\n<\/pre>\n<p><\/code><br \/>\n\u3000<br \/>\nPerl\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u5b9f\u884c\u3057\u3001\u53d6\u5f97\u3057\u305fURI\u3067\u66f8\u7c4d\u60c5\u5831XML\u3092\u53d6\u5f97\u3059\u308bPython\u30b9\u30af\u30ea\u30d7\u30c8\u3002<br \/>\nRequestTest.py:<br \/>\n<code><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#\/usr\/bin\/python\r\n# -*- coding: utf-8 -*-\r\nimport commands, urllib\r\n\r\nisbn = &#039;4091400019&#039;\r\n\r\n#Perl\u306eURI\u751f\u6210\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u5b9f\u884c\u3057\u3066\u6a19\u6e96\u5165\u529b\u304b\u3089URI\u3092\u53d6\u5f97\r\nrequestURI = commands.getoutput(&#039;perl .\/AmazonSignedRequestGenerator.pl %s&#039; % (isbn))\r\n#\u66f8\u7c4d\u60c5\u5831XML\u3092\u53d6\u5f97\r\nbookDataXML = urllib.urlopen(requestURI).read()\r\n\r\n#\u53d6\u5f97\u3057\u305fXML\u3092\u51fa\u529b\r\nprint bookDataXML\r\n<\/pre>\n<p><\/code><\/p>\n<p>\u3053\u3093\u306a\u611f\u3058\u3067\u3001Python\u304b\u3089Perl\u3092\u547c\u3073\u51fa\u3057\u3066\u66f8\u7c4d\u60c5\u5831\u3092\u53d6\u5f97\u3067\u304d\u307e\u3059\u3002<br \/>\nRequestTest.py\u306e8\u884c\u76ee\u3067AmazonSignedRequestGenerator.pl\u3092\u547c\u3073\u51fa\u3057\u3066\u3044\u308b\u3068\u3053\u308d\u304c\u30dd\u30a4\u30f3\u30c8\u3067\u3059\u3002<br \/>\n\u203b\u3053\u308c\u3089\u306e\u30bd\u30fc\u30b9\u306f\u30b5\u30f3\u30d7\u30eb\u306a\u306e\u3067\u3001\u4f7f\u7528\u3059\u308b\u5834\u5408\u306f\u9069\u5b9c\u30a8\u30e9\u30fc\u51e6\u7406\u3092\u884c\u3063\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p>Amazon\u30aa\u30d5\u30a3\u30b7\u30e3\u30eb\u30b5\u30a4\u30c8<br \/>\n<a href=\"http:\/\/docs.amazonwebservices.com\/AWSECommerceService\/latest\/DG\/index.html?RequestAuthenticationArticle.html\"\u3000 target=\"_blank\">Amazon Product Advertising API<\/a>\u3000<br \/>\n<a href=\"http:\/\/affiliate.amazon.co.jp\/gp\/associates\/help\/t126\/\" target=\"_blank\">Amazon \u30a2\u30bd\u30b7\u30a8\u30a4\u30c8\uff08\u30a2\u30d5\u30a3\u30ea\u30a8\u30a4\u30c8\uff09 &#8211; \u30d8\u30eb\u30d7<\/a>\u3000<br \/>\n\u53c2\u8003\u30b5\u30a4\u30c8<br \/>\n<a href=\"http:\/\/chalow.net\/2009-08-01-1.html\" target=\"_blank\">[\u3092] Perl \u306b\u3088\u308b\u81ea\u5206\u7528 Amazon Product Advertising API proxy\u300cPAPAXY\u300d<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Amazon Product Advertising API \u3078\u306e\u30ea\u30af\u30a8\u30b9\u30c8URI\u306e\u30d5\u30a9\u30fc\u30de\u30c3\u30c8\u304c\u5909\u66f4\u3055\u308c\u307e\u3057\u305f\u3002 \u30ea\u30af\u30a8\u30b9\u30c8URI\u306b\u7f72\u540d\u306e\u4ed8\u52a0\u304c\u5fc5\u9808\u3068\u306a\u308a\u3001\u30ea\u30af\u30a8\u30b9\u30c8\u5074\u304c\u7f72\u540d\u3092\u751f\u6210\u3057\u306a\u304f\u3066\u306f\u3044\u3051\u307e\u305b\u3093\u3002 ex. htt [&hellip;]<\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-1288","post","type-post","status-publish","format-standard","hentry","category-tech"],"_links":{"self":[{"href":"https:\/\/blog.bitmeister.jp\/index.php?rest_route=\/wp\/v2\/posts\/1288","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bitmeister.jp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bitmeister.jp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bitmeister.jp\/index.php?rest_route=\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bitmeister.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1288"}],"version-history":[{"count":82,"href":"https:\/\/blog.bitmeister.jp\/index.php?rest_route=\/wp\/v2\/posts\/1288\/revisions"}],"predecessor-version":[{"id":1371,"href":"https:\/\/blog.bitmeister.jp\/index.php?rest_route=\/wp\/v2\/posts\/1288\/revisions\/1371"}],"wp:attachment":[{"href":"https:\/\/blog.bitmeister.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bitmeister.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bitmeister.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}