private String getHMACSHA1(final String method, final String url,
final Map<String, String> args, final AccessGrant token)
throws Exception {
if (config.get_consumerSecret().length() == 0) {
throw new SignatureException("Please check consumer secret");
}
boolean valid = MethodType.GET.toString().equals(method)
|| MethodType.PUT.toString().equals(method)
|| MethodType.POST.toString().equals(method);
if (!valid) {
throw new SignatureException("Invalid method type :" + method);
}
if (url.length() == 0) {
throw new SignatureException("Please check URL");
}
String key = HttpUtil.encodeURIComponent(config.get_consumerSecret())
+ "&";
if (token != null && token.getSecret() != null) {
key += HttpUtil.encodeURIComponent(token.getSecret());
}
try {
// get an hmac_sha1 key from the raw key bytes
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes("UTF-8"),
"HMAC-SHA1");
// get an hmac_sha1 Mac instance and initialize with the signing key
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(signingKey);
String data = HttpUtil.encodeURIComponent(method) + "&"
+ HttpUtil.encodeURIComponent(url) + "&"
+ HttpUtil.encodeURIComponent(HttpUtil.buildParams(args));
LOG.debug("Signature data : " + data);
// compute the hmac on input data bytes
byte[] rawHmac = mac.doFinal(data.getBytes("UTF-8"));
// base64-encode the hmac
LOG.debug("Encoding raw HMAC to Base64");
String sig = Base64.encodeBytes(rawHmac);
return sig;
} catch (Exception e) {
throw new SignatureException("Unable to generate HMAC-SHA1", e);
}
}