tag:crieit.net,2005:https://crieit.net/tags/PAAPI/feed 「PAAPI」の記事 - Crieit Crieitでタグ「PAAPI」に投稿された最近の記事 2020-03-07T17:25:02+09:00 https://crieit.net/tags/PAAPI/feed tag:crieit.net,2005:PublicArticle/15750 2020-03-07T17:25:02+09:00 2020-03-07T17:25:02+09:00 https://crieit.net/posts/PAAPI-v5-Node-js PAAPI v5に移行してみた: Node.js版 <p><a target="_blank" rel="nofollow noopener" href="https://tsundoku.site">積読ハウマッチ</a>でAmazonのPAAPIを使っているけど、<br /> v4が2020年3月9日に利用できなくなるため、移行してみたときの備忘録。</p> <p><a href="https://crieit.now.sh/upload_images/e5449de49d37c0a93ce3ada90eb537335e635a1adaf4b.png" target="_blank" rel="nofollow noopener"><img src="https://crieit.now.sh/upload_images/e5449de49d37c0a93ce3ada90eb537335e635a1adaf4b.png?mw=700" alt="スクリーンショット 2020-03-07 17.12.50.png" /></a></p> <p>以前は、<a target="_blank" rel="nofollow noopener" href="https://github.com/dmcquay/node-apac">node-apac</a>を使ってたけど、<a target="_blank" rel="nofollow noopener" href="https://github.com/dmcquay/node-apac/issues/97">対応はなさそう</a>。</p> <h2 id="Amazon側の移行手順"><a href="#Amazon%E5%81%B4%E3%81%AE%E7%A7%BB%E8%A1%8C%E6%89%8B%E9%A0%86">Amazon側の移行手順</a></h2> <p><a target="_blank" rel="nofollow noopener" href="https://affiliate.amazon.co.jp/help/node/topic/GZH32YX29UH5GACM">公式の移行手順</a>を参考に進める感じ。</p> <h3 id="ステップ1: 新しい認証情報を取得する"><a href="#%E3%82%B9%E3%83%86%E3%83%83%E3%83%971%EF%BC%9A+%E6%96%B0%E3%81%97%E3%81%84%E8%AA%8D%E8%A8%BC%E6%83%85%E5%A0%B1%E3%82%92%E5%8F%96%E5%BE%97%E3%81%99%E3%82%8B">ステップ1: 新しい認証情報を取得する</a></h3> <p>認証情報の形式が古いと払い出しが必要らしい<br /> いままで使っていたアクセスキーとシークレットキーでOKだったのでスキップ</p> <h3 id="ステップ2: PA-API 5.0を試してみる"><a href="#%E3%82%B9%E3%83%86%E3%83%83%E3%83%972%EF%BC%9A+PA-API+5.0%E3%82%92%E8%A9%A6%E3%81%97%E3%81%A6%E3%81%BF%E3%82%8B">ステップ2: PA-API 5.0を試してみる</a></h3> <p>以前と同様、Scratchpadが用意されている。<br /> アクセスキーがOKだったかも、ここで確認した。<br /> ・<a target="_blank" rel="nofollow noopener" href="https://webservices.amazon.com/paapi5/scratchpad/index.html">Product Advertising API 5.0 Scratchpad</a></p> <h3 id="ステップ3: 必要な知識を得る"><a href="#%E3%82%B9%E3%83%86%E3%83%83%E3%83%973%EF%BC%9A+%E5%BF%85%E8%A6%81%E3%81%AA%E7%9F%A5%E8%AD%98%E3%82%92%E5%BE%97%E3%82%8B">ステップ3: 必要な知識を得る</a></h3> <p>公式ドキュメントにも書いてあるとおり、レスポンスがXMLからJSONになったらしい。</p> <p>変更点は、<a target="_blank" rel="nofollow noopener" href="https://webservices.amazon.com/paapi5/documentation/migration-guide/whats-new-in-paapi5.html">ここ</a>にまとまっている。<br /> 主には、セキュリティ強化、レスポンス速度の改善、新しいAPIの追加など。</p> <p>また、レスポンスに関してv4からv5へのマッピングも用意されてる。<br /> ・<a target="_blank" rel="nofollow noopener" href="https://webservices.amazon.com/paapi5/documentation/migration-guide/pa-api-40-to-50-mapping.html">PA-API 4.0 to 5.0 Mapping · Product Advertising API 5.0</a></p> <h3 id="ステップ4: PA-API 5.0を導入する"><a href="#%E3%82%B9%E3%83%86%E3%83%83%E3%83%974%EF%BC%9A+PA-API+5.0%E3%82%92%E5%B0%8E%E5%85%A5%E3%81%99%E3%82%8B">ステップ4: PA-API 5.0を導入する</a></h3> <p>従来どおり、Web APIを直接叩けるけど、SDKも提供されている。<br /> 言語は、Java / Node.js / PHP / Pythonの4つ。サンプルもある。<br /> ・<a target="_blank" rel="nofollow noopener" href="https://webservices.amazon.com/paapi5/documentation/quick-start/using-sdk.html">Using SDK · Product Advertising API 5.0</a></p> <p>ただ、残念なことに、Node.jsのSDKは、zipで公開されている...</p> <h2 id="ソース側の移行手順"><a href="#%E3%82%BD%E3%83%BC%E3%82%B9%E5%81%B4%E3%81%AE%E7%A7%BB%E8%A1%8C%E6%89%8B%E9%A0%86">ソース側の移行手順</a></h2> <h3 id="SDKを配置"><a href="#SDK%E3%82%92%E9%85%8D%E7%BD%AE">SDKを配置</a></h3> <p>zipをダウンロードして展開するとこんな感じになってる。</p> <p><a href="https://crieit.now.sh/upload_images/07628c2b1f8ec3ae587e9d119d743d6a5e635a2951165.png" target="_blank" rel="nofollow noopener"><img src="https://crieit.now.sh/upload_images/07628c2b1f8ec3ae587e9d119d743d6a5e635a2951165.png?mw=700" alt="スクリーンショット 2020-03-07 17.11.09.png" /></a></p> <p>src配下がAPIなので、自分のソースに配置。</p> <p><a href="https://crieit.now.sh/upload_images/35f1b70ec8ce6466124c642bb991527e5e635a37ecdf3.png" target="_blank" rel="nofollow noopener"><img src="https://crieit.now.sh/upload_images/35f1b70ec8ce6466124c642bb991527e5e635a37ecdf3.png?mw=700" alt="スクリーンショット 2020-03-07 6.06.05.png" /></a></p> <p>dependencyがあるので、それもコピペして、<code>npm i</code>する</p> <pre><code class="json">"dependencies": { "superagent": "3.5.2", "crypto-js": "^3.1.9-1" }, </code></pre> <h3 id="APIの呼び出し"><a href="#API%E3%81%AE%E5%91%BC%E3%81%B3%E5%87%BA%E3%81%97">APIの呼び出し</a></h3> <p>解答したzipにサンプルがあるので、それを見ながら実装していく。<br /> 使っているのは検索なので、<code>sampleSearchItemsApi.js</code>を参照。</p> <p>以下は、サンプルの抜粋</p> <pre><code class="javascript">// ** API Clientの準備 var ProductAdvertisingAPIv1 = require('./src/index'); var defaultClient = ProductAdvertisingAPIv1.ApiClient.instance; // アクセスキーなどを設定。ホストとかリージョンは、以下を参照 // https://webservices.amazon.com/paapi5/documentation/common-request-parameters.html#host-and-region defaultClient.accessKey = '<YOUR ACCESS KEY>'; defaultClient.secretKey = '<YOUR SECRET KEY>'; defaultClient.host = 'webservices.amazon.co.jp'; defaultClient.region = 'us-west-2'; // ** リクエストの生成 var searchItemsRequest = new ProductAdvertisingAPIv1.SearchItemsRequest(); // アソシエイトタグを設定 searchItemsRequest['PartnerTag'] = '<YOUR PARTNER TAG>'; searchItemsRequest['PartnerType'] = 'Associates'; // 検索リクエストを設定。SearchIndexは以下を参照 // https://webservices.amazon.com/paapi5/documentation/use-cases/organization-of-items-on-amazon/search-index.html searchItemsRequest['Keywords'] = 'Harry Potter'; searchItemsRequest['SearchIndex'] = 'Books'; searchItemsRequest['ItemCount'] = 2; // 取得するレスポンスの指定 // https://webservices.amazon.com/paapi5/documentation/search-items.html#resources-parameter searchItemsRequest['Resources'] = ['Images.Primary.Medium', 'ItemInfo.Title', 'Offers.Listings.Price']; // ** コールバックの設定 var callback = function (error, data, response) { if (error) { // エラーの場合 console.log('Status Code: ' + error['status']); } else { // レスポンスを取得 var searchItemsResponse = ProductAdvertisingAPIv1.SearchItemsResponse.constructFromObject(data); if (searchItemsResponse['SearchResult'] !== undefined) { // 1件目の結果を取得 var item_0 = searchItemsResponse['SearchResult']['Items'][0]; if (item_0 !== undefined && item_0['ASIN'] !== undefined) { console.log('ASIN: ' + item_0['ASIN']); } } // エラーが返ってきた場合 if (searchItemsResponse['Errors'] !== undefined) { var error_0 = searchItemsResponse['Errors'][0]; console.log('Error Code: ' + error_0['Code']); console.log('Error Message: ' + error_0['Message']); } } }; // ** APIの実行 var api = new ProductAdvertisingAPIv1.DefaultApi(); try { api.searchItems(searchItemsRequest, callback); } catch (ex) { console.log('Exception: ' + ex); } </code></pre> <h2 id="対応が必要だった点"><a href="#%E5%AF%BE%E5%BF%9C%E3%81%8C%E5%BF%85%E8%A6%81%E3%81%A0%E3%81%A3%E3%81%9F%E7%82%B9">対応が必要だった点</a></h2> <p>基本的にパラメタがわかったため、一致してるものを探していけばOK。<br /> 変更自体はそこまでだけど、できることが変わっているので注意...</p> <h3 id="リクエスト関連"><a href="#%E3%83%AA%E3%82%AF%E3%82%A8%E3%82%B9%E3%83%88%E9%96%A2%E9%80%A3">リクエスト関連</a></h3> <ul> <li>MerchantIdがMerchantになっていた。設定値は変わらず</li> <li>SortがSortyByになり、設定値も変更</li> <li>PowerSearchが廃止に...つまり、除外や期間指定ができない</li> </ul> <h3 id="レスポンス関連"><a href="#%E3%83%AC%E3%82%B9%E3%83%9D%E3%83%B3%E3%82%B9%E9%96%A2%E9%80%A3">レスポンス関連</a></h3> <ul> <li>全体的に変更。XMLからJSONになっており、構成も変わっている</li> <li>発売日がUTCになり、時刻まで記載</li> <li>エラーメッセージも全体的に変わっているので対応が必要</li> <li>バリエーションが除外され、GetVariations APIが新設(リクエスト数が増...)</li> <li>ただ、電子書籍の価格が取得できるようになった</li> </ul> <h2 id="よく参照したURL"><a href="#%E3%82%88%E3%81%8F%E5%8F%82%E7%85%A7%E3%81%97%E3%81%9FURL">よく参照したURL</a></h2> <p>とにかく、以下を読みまくった。。</p> <ul> <li>Operations関連: リクエストパラメタとか。指定できるレスポンスもここ <ul> <li><a target="_blank" rel="nofollow noopener" href="https://webservices.amazon.com/paapi5/documentation/search-items.html">SearchItems</a> ... 検索</li> <li><a target="_blank" rel="nofollow noopener" href="https://webservices.amazon.com/paapi5/documentation/get-items.html">GetItems</a> ... ASINから商品情報取得</li> <li><a target="_blank" rel="nofollow noopener" href="https://webservices.amazon.com/paapi5/documentation/get-variations.html">GetVariations</a> ... ASINからバリエーション情報取得</li> </ul></li> <li>Resource関連: レスポンスの内容 <ul> <li><a target="_blank" rel="nofollow noopener" href="https://webservices.amazon.com/paapi5/documentation/item-info.html">ItemInfo</a> ... 商品情報</li> <li><a target="_blank" rel="nofollow noopener" href="https://webservices.amazon.com/paapi5/documentation/images.html">Images</a> ... 画像関連</li> <li><a target="_blank" rel="nofollow noopener" href="https://webservices.amazon.com/paapi5/documentation/offers.html">Offers</a> ... 価格関連</li> </ul></li> <li>共通のリクエストパラメタ関連 <ul> <li><a target="_blank" rel="nofollow noopener" href="https://webservices.amazon.com/paapi5/documentation/locale-reference/japan.html">日本のMarketplaceとかの情報</a></li> <li><a target="_blank" rel="nofollow noopener" href="https://webservices.amazon.com/paapi5/documentation/common-request-parameters.html#host-and-region">共通のリクエストパラメタ</a> ... HostやRegionもここ</li> </ul></li> <li>共通のレスポンス関連 <ul> <li><a target="_blank" rel="nofollow noopener" href="https://webservices.amazon.com/paapi5/documentation/troubleshooting/error-messages.html">エラーメッセージ</a></li> </ul></li> <li>マイグレーション関連 <ul> <li><a target="_blank" rel="nofollow noopener" href="https://affiliate.amazon.co.jp/help/node/topic/GZH32YX29UH5GACM">v5.0への移行ガイドの日本語版</a></li> <li><a target="_blank" rel="nofollow noopener" href="https://webservices.amazon.com/paapi5/documentation/migration-guide/pa-api-40-to-50-mapping.html">v4.0からv5.0へのマッピング</a></li> <li><a target="_blank" rel="nofollow noopener" href="https://webservices.amazon.com/paapi5/documentation/play-around-using-scratchpad.html">Scratchpadの使い方</a></li> </ul></li> </ul> <p>以上!!</p> <h2 id="こんなのつくってます!!"><a href="#%E3%81%93%E3%82%93%E3%81%AA%E3%81%AE%E3%81%A4%E3%81%8F%E3%81%A3%E3%81%A6%E3%81%BE%E3%81%99%21%21">こんなのつくってます!!</a></h2> <p>積読用の読書管理アプリ 『積読ハウマッチ』をリリースしました!<br /> <a target="_blank" rel="nofollow noopener" href="https://tsundoku.site">積読ハウマッチ</a>は、Nuxt.js+Firebaseで開発してます!</p> <p><img src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/478782/572d4947-f40b-e4dc-1c9c-bc584cd2a66c.png" width="200"/></p> <p>もしよかったら、遊んでみてくださいヽ(=´▽`=)ノ</p> <p>要望・感想・アドバイスなどあれば、<br /> 公式アカウント(<a target="_blank" rel="nofollow noopener" href="https://twitter.com/MemoryLoverz">@MemoryLoverz</a>)や開発者(<a target="_blank" rel="nofollow noopener" href="https://twitter.com/kira_puka">@kira_puka</a>)まで♪</p> きらぷか@積読ハウマッチ/SSSAPIなど