Skip to content

Instantly share code, notes, and snippets.

@knitfaced
Created November 1, 2010 21:46
Show Gist options
  • Save knitfaced/658929 to your computer and use it in GitHub Desktop.
Save knitfaced/658929 to your computer and use it in GitHub Desktop.
snippet to connect to the Ravelry API
private String createShopSearchQuery() throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException {
String jsonAction = "http://api.ravelry.com/shops/search.json?";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ");
Calendar now = Calendar.getInstance();
String timestamp = df.format(now.getTime());
String searchTerm = "yarn";
int shop_type_id = 1;
String query = jsonAction;
query += "access_key=" + urlEncode(ACCESS_KEY);
query += "&query=" + urlEncode(searchTerm);
query += "&shop_type_id=" + shop_type_id;
query += "&timestamp=" + urlEncode(timestamp);
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(SECRET_KEY.getBytes(), mac.getAlgorithm()));
byte[] signature = mac.doFinal(query.getBytes());
byte[] encryptedSignature = Base64.encodeBase64(signature);
query += "&signature=" + urlEncode(new String(encryptedSignature));
return query;
}
private String urlEncode(String param) throws UnsupportedEncodingException {
return URLEncoder.encode(param, "UTF-8");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment