-
-
Save underhilllabs/1933925 to your computer and use it in GitHub Desktop.
snippet to connect to the Ravelry API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 += "×tamp=" + 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