重要:2009年8月15日をもって、Product Advertising API は、署名認証を含まないリクエストを一切処理しなくなります。現在、Product Advertising API を利用されている方は、今すぐご利用中のすべてのリクエストに署名認証を含める準備を開始いただけますようお願いいたします。(略)

Product Advertising API(リンク作成用API、旧称:AmazonアソシエイトWebサービス)

ということなので、署名認証付きでAPIを利用するサンプルを書いてみた。

事前準備

署名認証するには、「Access Key ID」と「Secret Access Key」が必要。
Amazon Web Services にてログインしてメニューから [Your Account] -> [Access Identifiers] へ移動すると、「Access Key ID」と「Secret Access Key」を見ることができる。

サンプルクラス SignedRequestsHelper

Product Advertising API - Developer Guide (API Version 2009-07-01) - Java Sample Code for Calculating Signature Version 2 Signatures にあるサンプルコードを日本向けに修正したサンプル。
実質的には ecs.amazonaws.com を ecs.amazonaws.jp へ変更しただけ。
実際に使う際には変数 awsAccessKeyId と awsSecretKey を自分用 Access Key ID と Secret Access Key へ書き換える必要がある。

ライブラリとして Apache Commons Codec が必要。


import java.io.UnsupportedEncodingException;
 
import java.net.URLDecoder;
import java.net.URLEncoder;
 
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
 
import java.text.DateFormat;
import java.text.SimpleDateFormat;
 
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.SortedMap;
import java.util.TimeZone;
import java.util.TreeMap;
 
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
 
import org.apache.commons.codec.binary.Base64;
 
public class SignedRequestsHelper {
  private static final String UTF8_CHARSET = "UTF-8";
  private static final String HMAC_SHA256_ALGORITHM = "HmacSHA256";
  private static final String REQUEST_URI = "/onca/xml";
  private static final String REQUEST_METHOD = "GET";
 
  // TODO: ecs.amazonaws.com -> ecs.amazonaws.jp
  private String endpoint = "ecs.amazonaws.jp"; // must be lowercase
  // TODO: Access Key ID
  private String awsAccessKeyId = "YOUR AWS ACCESS KEY";
  // TODO: Secret Access Key
  private String awsSecretKey = "YOUR AWS SECRET KEY";
 
  private SecretKeySpec secretKeySpec = null;
  private Mac mac = null;
 
  public SignedRequestsHelper() throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
    byte[] secretyKeyBytes = awsSecretKey.getBytes(UTF8_CHARSET);
    secretKeySpec =
      new SecretKeySpec(secretyKeyBytes, HMAC_SHA256_ALGORITHM);
    mac = Mac.getInstance(HMAC_SHA256_ALGORITHM);
    mac.init(secretKeySpec);
  }
 
  public String sign(Map<String, String> params) {
    params.put("AWSAccessKeyId", awsAccessKeyId);
    params.put("Timestamp", timestamp());
 
    SortedMap<String, String> sortedParamMap =
      new TreeMap<String, String>(params);
    String canonicalQS = canonicalize(sortedParamMap);
    String toSign =
      REQUEST_METHOD + "\n"
      + endpoint + "\n"
      + REQUEST_URI + "\n"
      + canonicalQS;
 
    String hmac = hmac(toSign);
    String sig = percentEncodeRfc3986(hmac);
    String url = "http://" + endpoint + REQUEST_URI + "?" +
    canonicalQS + "&Signature=" + sig;
 
    return url;
  }
 
  private String hmac(String stringToSign) {
    String signature = null;
    byte[] data;
    byte[] rawHmac;
    try {
      data = stringToSign.getBytes(UTF8_CHARSET);
      rawHmac = mac.doFinal(data);
      Base64 encoder = new Base64();
      signature = new String(encoder.encode(rawHmac));
    } catch (UnsupportedEncodingException e) {
      throw new RuntimeException(UTF8_CHARSET + " is unsupported!", e);
    }
    return signature;
  }
 
  private String timestamp() {
    String timestamp = null;
    Calendar cal = Calendar.getInstance();
    DateFormat dfm = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    dfm.setTimeZone(TimeZone.getTimeZone("GMT"));
    timestamp = dfm.format(cal.getTime());
    return timestamp;
  }
 
  private String canonicalize(SortedMap<String, String> sortedParamMap)
{
    if (sortedParamMap.isEmpty()) {
      return "";
    }
 
    StringBuffer buffer = new StringBuffer();
    Iterator<Map.Entry<String, String>> iter =
      sortedParamMap.entrySet().iterator();
 
    while (iter.hasNext()) {
      Map.Entry<String, String> kvpair = iter.next();
      buffer.append(percentEncodeRfc3986(kvpair.getKey()));
      buffer.append("=");
      buffer.append(percentEncodeRfc3986(kvpair.getValue()));
      if (iter.hasNext()) {
        buffer.append("&");
      }
    }
    String cannoical = buffer.toString();
    return cannoical;
  }
 
  private String percentEncodeRfc3986(String s) {
    String out;
    try {
      out = URLEncoder.encode(s, UTF8_CHARSET)
      .replace("+", "%20")
      .replace("*", "%2A")
      .replace("%7E", "~");
    } catch (UnsupportedEncodingException e) {
      out = s;
    }
    return out;
  }
 
}

Ref.
-試験管のなかのコード :: 「Amazon アソシエイト Web サービス」改め「Product Advertising API」メモ
-The net is vast: Product Advertising APIの日本用Restクライアント(Java)

また、Timestamp の値は GMT でも JST でもどちらでも動作したので、このサンプルコードではそのまま GMT にしている。(APIコール結果に含まれるTimestampの値が指定した通りにセットされていて問題なく動作しているように見えたし)。

* Signature (署名) - このパラメータはオプションとなり、初期値は存在しません。Signatureパラメータはリクエストのタイプ、ドメイン、URLを使用して作成され、パラメータは、<parameter>=<value>& のフォーマットで記述されており、リクエスト内の各パラメータの文字列の順に並べられています(Signatureパラメータ本体以外)。正しい仕様になっていれば、AWS秘密キーを元にbase64エンコードのHMAC-SHA256署名が作成されます。このプロセスに関する詳細は、RESTリクエストのサンプルをご参照ください。
* Timestamp (タイムスタンプ) - このパラメータは、Signatureパラメータをリクエストに含める場合は必須となり、含めていない場合はオプションとなります。こちらについても初期値は存在しません。リクエストに含めるタイムスタンプは、dateTimeオブジェクトである必要があり、すなわち、date オブジェクトおよびtime オブジェクトの全ての情報がを含めたもの(年月日・時分秒)となります(こちらに関するより詳細な情報については、http://www.w3.org/TR/NOTE-datetimeをご参照ください)。この値はISO8601で定義された国際標準時(GMT)での表示形式となります: YYYY-MM-DDThh:mm:ssZ (T および Z はリテラル値)

Amazon アソシエイト(アフィリエイト) - ヘルプ - 認証パラメータ

サンプルクラス SignedRequestsHelperSample

SignedRequestsHelper を利用するサンプル。


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.*;
import java.util.*;
 
public class SignedRequestsHelperSample {
 
  public static void main(String[] args) throws Exception {
    Map<String, String> params = new HashMap<String, String>();
    params.put("Service", "AWSECommerceService");
    params.put("Operation", "ItemSearch");
    params.put("ResponseGroup", "Request,Medium");
    params.put("Keywords", "人間失格");
    params.put("SearchIndex", "Blended");
    SignedRequestsHelper srh = new SignedRequestsHelper();
    String url = srh.sign(params);
    String content = getContent(new URL(url));
    System.out.println(content);
  }
 
  // URLからコンテンツ(HTML/XMLページの文字列)を取得
  private static String getContent(URL url) throws Exception{
    
    HttpURLConnection con = (HttpURLConnection)url.openConnection();
    con.setRequestMethod("GET");
    InputStream is = con.getInputStream();
    InputStreamReader isr = new InputStreamReader(is, "UTF-8");
    BufferedReader br = new BufferedReader(isr);
    StringBuffer buf = new StringBuffer();
    String s;
    while ((s = br.readLine()) != null) {
      buf.append(s);
      buf.append("\r\n");
    }
 
    br.close();
    con.disconnect();
    
    return buf.toString();
  }
}

Ref.
-Product Advertising API(リンク作成用API、旧称:AmazonアソシエイトWebサービス)
-Amazon アソシエイト(アフィリエイト) - ヘルプ - Product Advertising API 開発者向けガイド リクエストの署名認証について(参考訳)
-Product Advertising API - Example REST Requests
-Product Advertising API - Request Authentication
-AWS (Amazon Web Services) (NI-Lab.'s MemoWiki - AmazonWebServices)

tags: zlashdot WebServices AmazonWebServices

Posted by NI-Lab. (@nilab)