Java Sample Code For Calculating Signature Version 2 Signatures - Amazon Product Advertising API Developer's Manual

Api version 2011-08-01
Table of Contents

Advertisement

Product Advertising API Developer Guide
Note
HTTPRequestURI is always "/onca/xml" for Product Advertising API. HTTPVerb is either
GET or POST.
3.
Calculate an RFC 2104-compliant HMAC with the string you just created, your Secret Access Key
as the key, and SHA256 as the hash algorithm.
For more information, go to http://tools.ietf.org/html/rfc2104.
4.
Convert the resulting value to base64.
5.
Use the resulting value as the value of the Signature request parameter.
Important
The final signature you send in the request must be URL encoded as specified in RFC 3986 (for
more information, go to http://tools.ietf.org/html/rfc3986). If your toolkit URL encodes your final
request, then it handles the required URL encoding of the signature. If your toolkit doesn't URL
encode the final request, then make sure to URL encode the signature before you include it in
the request. Most importantly, make sure the signature is URL encoded only once. A common
mistake is to URL encode it manually during signature formation, and then again when the toolkit
URL encodes the entire request.

Java Sample Code for Calculating Signature Version 2 Signatures

The following Java code sample shows how to calculate a signature (version 2). This sample has code
for creating the canonical string, for base64 encoding, and for HMAC encoding for generating the signature.
package com.amazon.associates.sample;
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";
Request Authentication
API Version 2011-08-01
59

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the Product Advertising API and is the answer not in the manual?

Questions and answers

Subscribe to Our Youtube Channel

Table of Contents