Last active
December 14, 2015 02:59
-
-
Save urlbox/5017518 to your computer and use it in GitHub Desktop.
urlbox java
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
import java.io.UnsupportedEncodingException; | |
import java.math.BigInteger; | |
import java.net.URLEncoder; | |
import java.security.InvalidKeyException; | |
import java.security.NoSuchAlgorithmException; | |
import java.util.Map; | |
import java.util.HashMap; | |
import javax.crypto.Mac; | |
import javax.crypto.spec.SecretKeySpec; | |
public class Urlbox { | |
private String key; | |
private String secret; | |
Urlbox(String api_key, String api_secret) { | |
this.key = api_key; | |
this.secret = api_secret; | |
} | |
// main method demos Example Usage | |
public static void main(String[] args) { | |
String urlboxKey = "your-urlbox-api-key"; | |
String urlboxSecret = "your-urlbox-secret"; | |
// Set request options | |
Map<String, Object> options = new HashMap<String, Object>(); | |
options.put("width", 1280); | |
options.put("height", 1024); | |
// options.put("wrap", "iphone5"); | |
options.put("thumb_width", 240); | |
options.put("full_page", "false"); | |
options.put("force", "false"); | |
// Create urlbox object with api key and secret | |
Urlbox urlbox = new Urlbox(urlboxKey, urlboxSecret); | |
try { | |
// Call generateUrl function of urlbox object | |
String urlboxUrl = urlbox.generateUrl("bbc.co.uk", options); | |
// Now do something with urlboxUrl.. put in img tag, etc.. | |
} catch (UnsupportedEncodingException ex) { | |
throw new RuntimeException("Problem with url encoding", ex); | |
} | |
} | |
public String generateUrl(String url, Map<String,Object> options) throws UnsupportedEncodingException { | |
String encodedUrl = URLEncoder.encode(url, "UTF-8"); | |
String queryString = String.format("url=%s", encodedUrl); | |
for (Map.Entry<String, Object> entry : options.entrySet()) { | |
String queryParam = "&"+entry.getKey()+"="+entry.getValue(); | |
queryString += queryParam; | |
} | |
String token = generateToken(queryString, this.secret); | |
String result = String.format("https://api.urlbox.io/v1/%s/%s/png?%s", this.key, token, queryString); | |
System.out.println(result); | |
return result; | |
} | |
private String generateToken(String input, String key) { | |
String lSignature = "None"; | |
try { | |
Mac lMac = Mac.getInstance("HmacSHA1"); | |
SecretKeySpec lSecret = new SecretKeySpec(key.getBytes(), "HmacSHA1"); | |
lMac.init(lSecret); | |
byte[] lDigest = lMac.doFinal(input.getBytes()); | |
BigInteger lHash = new BigInteger(1, lDigest); | |
lSignature = lHash.toString(16); | |
if ((lSignature.length() % 2) != 0) { | |
lSignature = "0" + lSignature; | |
} | |
} catch (NoSuchAlgorithmException lEx) { | |
throw new RuntimeException("Problems calculating HMAC", lEx); | |
} catch (InvalidKeyException lEx) { | |
throw new RuntimeException("Problems calculating HMAC", lEx); | |
} | |
return lSignature; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment