Difference between revisions of "Template:Json API HMAC Block- Java"
From Updox API
Line 1: | Line 1: | ||
public static void generateHmacHeaders(HttpURLConnection connection) throws Exception{ | public static void generateHmacHeaders(HttpURLConnection connection) throws Exception{ | ||
− | final SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss (z)"); | + | <span style="color:green"> // Create the date string</span> |
+ | final SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss (z)"); | ||
d.setTimeZone(TimeZone.getTimeZone("GMT")); | d.setTimeZone(TimeZone.getTimeZone("GMT")); | ||
final String timestampString = d.format(new Date()); | final String timestampString = d.format(new Date()); | ||
− | final String message = applicationId + ":" + applicationPassword + ":" + timestampString;<span style="color:green">// Create the | + | <span style="color:green"> // Create the message</span> |
− | final String auth = "HMAC " + hmacSha1ToBase64(message, apiSecret); <span style="color:green">// | + | final String message = applicationId + ":" + applicationPassword + ":" + timestampString; |
− | connection.setRequestProperty("updox-timestamp", timestampString); | + | <span style="color:green"> // Create the authorization hash</span> |
+ | final String auth = "HMAC " + hmacSha1ToBase64(message, apiSecret); | ||
+ | <span style="color:green"> // Set the request headers</span> | ||
+ | connection.setRequestProperty("updox-timestamp", timestampString); | ||
connection.setRequestProperty("Authorization", auth); | connection.setRequestProperty("Authorization", auth); | ||
} | } | ||
{{Json HMACtoBASE64 Block-Java}} | {{Json HMACtoBASE64 Block-Java}} |
Revision as of 16:19, 27 November 2013
public static void generateHmacHeaders(HttpURLConnection connection) throws Exception{
// Create the date string
final SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss (z)");
d.setTimeZone(TimeZone.getTimeZone("GMT"));
final String timestampString = d.format(new Date());
// Create the message
final String message = applicationId + ":" + applicationPassword + ":" + timestampString;
// Create the authorization hash
final String auth = "HMAC " + hmacSha1ToBase64(message, apiSecret);
// Set the request headers
connection.setRequestProperty("updox-timestamp", timestampString); connection.setRequestProperty("Authorization", auth); } public static String hmacSha1ToBase64(String value, String key) throws Exception{ byte[] keyBytes = key.getBytes(); SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA1"); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(signingKey); byte[] rawHmac = mac.doFinal(value.getBytes()); byte[] base64Bytes = Base64.encodeBase64(rawHmac); return new String(base64Bytes, "UTF-8"); }