LoginSignup
5
5

More than 5 years have passed since last update.

JAVAでAmazon Product Advertising APIを使用するときのメモ(2016/11時点)

Last updated at Posted at 2016-11-23

JAVAでAmazon Product Advertising APIを使用するときの手順・サンプルコードをメモ書きします。なお、当該APIは今後仕様変更の可能性もあるので、今回のメモは2016/11時点での内容とします。

1.アカウント作成

amazonのサイトでアカウント登録をします。Amazon Product Advertising API関連リンク集の記事が参考になります。

2.ヘルパークラス・サンプルコードの取得

Product Advertising API Signed Requests Sample Code - Java REST/QUERYよりコードをダウンロードします。

3.APIリクエスト先のURL確認

Anatomy of a REST RequestからAPIのリクエスト先のURLを確認します。

4.APIリクエストのコード例

以下に歌手名で音楽を検索する場合を例に、サンプルコードを記載します。なお、パラメータのリファレンスはProduct Advertising APIのページに詳しく記載されています。

AmazonApiSample.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class AmazonApiSample {

    public static String searchMusic(String artist) {
        // ダウンロードしたヘルパークラスでオブジェクトの作成
        SignedRequestsHelper helper;
        try {
            // ENDPOINTに確認したURLのドメインを入力
            helper = SignedRequestsHelper.getInstance(AmazonConstants.ENDPOINT, AmazonConstants.AWS_ACCESS_KEY_ID, AmazonConstants.AWS_SECRET_KEY);
        } catch (Exception e) {
            // exceptionの時はnullを返す
            return null;
        }
        // Mapにリクエストのキー情報をつめる
        Map<String, String> params = new HashMap<String, String>();
        params.put("Service", "AWSECommerceService");
        params.put("AWSAccessKeyId", AmazonConstants.AWS_ACCESS_KEY_ID);
        params.put("Version", "2013-08-01");
        params.put("AssociateTag", AmazonConstants.AWS_ASSOCIATETAG);
        params.put("Operation", "ItemSearch");
        params.put("SearchIndex", "Music");
        params.put("Artist", artist);
        params.put("Sort", "-releasedate");
        params.put("ContentType", "text/xml");

        HttpURLConnection httpURLConnection = null;
        BufferedReader bufferedReader = null;

        try{
            // リクエスト処理
            String requestUrl = helper.sign(params);
            URL url = new URL(requestUrl);
            httpURLConnection = (HttpURLConnection)url.openConnection();
            String charSet = "UTF-8";
            String method = "GET";
            httpURLConnection.setRequestMethod( method );
            InputStreamReader inputStreamReader = new InputStreamReader( httpURLConnection.getInputStream(), charSet );
            bufferedReader = new BufferedReader( inputStreamReader );

            // レスポンスのXMLを取得。
            String oneLine = null;
            String responseXml = "";
            while( true ){
                oneLine = bufferedReader.readLine();
                // 行がNULLの場合
                if(oneLine == null){
                    break;
                // レスポンスの文字列へ行を追加
                }else{
                    responseXml += oneLine;
                }
            }
            return responseXml;
        }catch( Exception e ){
            // exceptionの時はnullを返す
            return null;
        }finally{

            if( bufferedReader != null ){
                try{
                    bufferedReader.close();
                }catch( IOException e ){
                    // エラー時は無処理
                }
            }
            if( httpURLConnection != null ){
                httpURLConnection.disconnect();
            }
        }
    }

}
5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5