Getting Started with Amazon Web Services in NetBeans IDE

When you need to connect to Amazon Web Services, NetBeans IDE gives you a nice start. You can drag and drop the "itemSearch" service into a Java source file and then various Amazon files are generated for you.

From there, you need to do a little bit of work because the request to Amazon needs to be signed before it can be used.

Here are some references and places that got me started:

You definitely need to sign up to the Amazon Associates program and also register/create an Access Key ID, which will also get you a Secret Key, as well.

Here's a simple Main class that I created that hooks into the generated RestConnection/RestResponse code created by NetBeans IDE:

public static void main(String[] args) {
    try {
        String searchIndex = "Books";
        String keywords = "Romeo and Juliet";
        RestResponse result = AmazonAssociatesService.itemSearch(searchIndex, keywords);
        String dataAsString = result.getDataAsString();
        int start = dataAsString.indexOf("<Author>")+8;
        int end = dataAsString.indexOf("</Author>");
        System.out.println(dataAsString.substring(start,end));
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

Then I deleted the generated properties file and the authenticator and changed the generated AmazonAssociatesService.java file to the following:

public class AmazonAssociatesService {
    private static void sleep(long millis) {
        try {
            Thread.sleep(millis);
        } catch (Throwable th) {
        }
    }
    public static RestResponse itemSearch(String searchIndex, String keywords) throws IOException {
        SignedRequestsHelper helper;
        RestConnection conn = null;
        Map queryMap = new HashMap();
        queryMap.put("Service", "AWSECommerceService");
        queryMap.put("AssociateTag", "myAssociateTag");
        queryMap.put("AWSAccessKeyId", "myAccessKeyId");
        queryMap.put("Operation", "ItemSearch");
        queryMap.put("SearchIndex", searchIndex);
        queryMap.put("Keywords", keywords);
        try {
            helper = SignedRequestsHelper.getInstance(
                    "ecs.amazonaws.com",
                    "
myAccessKeyId",
                    "mySecretKey");
            String sign = helper.sign(queryMap);
            conn = new RestConnection(sign);
        } catch (IllegalArgumentException | UnsupportedEncodingException | NoSuchAlgorithmException | InvalidKeyException ex) {
        }
        sleep(1000);
        return conn.get(null);
    }
}

Finally, I copied this class into my application, which you can see is referred to above:

http://code.google.com/p/amazon-product-advertising-api-sample/source/browse/src/com/amazon/advertising/api/sample/SignedRequestsHelper.java

Here's the completed app, mostly generated via the drag/drop shown at the start, but slightly edited as shown above:


That's all, now everything works as you'd expect.

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« September 2015
SunMonTueWedThuFriSat
  
2
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today