In this post, I will explain the simplest setup to use Amazon Product API in Java.
I know Amazon provides soap interface and we can automatically create Soap code for accessing Amazon Product API.
But sometimes, it is too much for light users. So I will show you the simplest way to use Amazon Product API using SignedRequestsHelper.
Hope this post helps your application development ;)
I know Amazon provides soap interface and we can automatically create Soap code for accessing Amazon Product API.
But sometimes, it is too much for light users. So I will show you the simplest way to use Amazon Product API using SignedRequestsHelper.
Hope this post helps your application development ;)
Preparation
- Create an AWS Account
- Get AWS Key, secret key and associate tag: I think you can create them from http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSGettingStartedGuide/AWSCredentials.html
- Get SignedRequestsHelper: You can download from https://code.google.com/p/amazon-product-advertising-api-sample/source/browse/src/com/amazon/advertising/api/sample/SignedRequestsHelper.java
Java Code Example
Congratulation! After you finish above preparation, now you can request Amazon Product API!!
The following code shows how to use SignedRequestsHelper.
The key points of the example code
- You should set your AWS key, secret key, and associate tag to the static variable in program (e.g. SECRET_KEY, AWS_KEY etc.)
- The example code takes multiple ASINs and response group type. In this example getting ItemAttributes as a response.
- You can test other response groups from Aws Documentation
- I have not tested but you can send another type of request (like item search) if you create proper XML request in createUrl method.
- I intentionally not to use external library. You can use XML library rather than Java standard XML API.
import java.io.FileNotFoundException; import java.io.IOException; import java.io.StringWriter; import java.util.HashMap; import java.util.Map; import java.util.Properties; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.xml.sax.SAXException; public class AmazonRestService { public static void main(String[] args) throws Exception { // create url // you can give multiple asins and response group explained in http://docs.aws.amazon.com/AWSECommerceService/latest/DG/CHAP_ResponseGroupsList.html String url = new AmazonRestService().createUrl(new String[]{"B00EPD3B2W", "B000099156"}, "ItemAttributes"); // send request and get xml response Document response = getResponse(url); // do something nice based on response! // in this example, simply print response printResponse(response); } // // Here you should put your keys. // private static final String SECRET_KEY = "your secret key"; private static final String AWS_KEY = "your aws key"; private static final String ASSOCIATE_TAG = "your associate tag"; private final static SignedRequestsHelper HELPER; static { try { HELPER = SignedRequestsHelper.getInstance("ecs.amazonaws.com", AWS_KEY, SECRET_KEY); } catch (Exception e) { throw new RuntimeException("Initilization Failed", e); } } public String createUrl(final String[] asins, final String responseGroup) throws ParserConfigurationException, IOException, SAXException { Map<String, String> params = new HashMap<String, String>(){{ put("Service", "AWSECommerceService"); put("Version", "2009-03-31"); put("Operation", "ItemLookup"); put("ItemId", join(asins, ",")); put("ResponseGroup", responseGroup); put("AssociateTag",ASSOCIATE_TAG); }}; return HELPER.sign(params); } // utility methods not so important part private static Document getResponse(String url) throws ParserConfigurationException, IOException, SAXException { DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = builder.parse(url); return doc; } private static void printResponse(Document doc) throws TransformerException, FileNotFoundException { Transformer trans = TransformerFactory.newInstance().newTransformer(); Properties props = new Properties(); props.put(OutputKeys.INDENT, "yes"); trans.setOutputProperties(props); StreamResult res = new StreamResult(new StringWriter()); DOMSource src = new DOMSource(doc); trans.transform(src, res); String toString = res.getWriter().toString(); System.out.println(toString); } private static String join(String[] pieces, String separator) { if(pieces.length == 0) return ""; StringBuilder sb = new StringBuilder(pieces[0]); for(int i = 1; i < pieces.length; i++) { sb.append(separator).append(pieces[i]); } return sb.toString(); } }
コメント