Skip to content

Instantly share code, notes, and snippets.

@tstachl
Created April 13, 2013 02:07
Show Gist options
  • Save tstachl/5376562 to your computer and use it in GitHub Desktop.
Save tstachl/5376562 to your computer and use it in GitHub Desktop.
Example talking to the Desk.com API from Salesforce (Apex).
public class DeskSample
{
private static String OAUTH_KEY = 'youroauthkey';
private static String OAUTH_SECRET = 'youroauthsecret';
private static String ACCESS_TOKEN = 'youraccesstoken';
private static String ACCESS_TOKEN_SECRET = 'youraccesstokensecret';
public static String DESK_SITENAME = 'yoursite';
public static Void doRequest()
{
HttpRequest req = new HttpRequest();
req.setMethod('GET');
req.setEndpoint('https://' + DeskSample.DESK_SITENAME + '.desk.com/api/v1/cases.json?count=10');
// req.setBody(''); // you can use that in combination with a POST request
// we need to sign the request
req = DeskSample.signRequest(req);
Http client = new Http();
try {
// execute the web service call
HttpResponse response = client.send(req);
// debug messages
System.debug(response.getBody());
System.debug('STATUS: ' + response.getStatus());
System.debug('STATUS CODE: ' + response.getStatusCode());
// process the response
// return the response
} catch(System.CalloutException e) {
System.debug('EXCEPTION THROWN: ' + e.getMessage());
}
}
private static Map<String, String> getParams(String paramString)
{
Map<String, String> params = new Map<String, String>();
if (paramString == null || paramString == '') return params;
for(String s : paramString.split('&')) {
String[] sl = s.split('=');
if (sl.size() == 2) {
params.put(sl[0], sl[1]);
}
}
return params;
}
private static HttpRequest signRequest(HttpRequest req)
{
Map<String, String> params = new Map<String, String>{
'oauth_consumer_key' => DeskSample.OAUTH_KEY,
'oauth_nonce' => String.valueOf(Crypto.getRandomLong()),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_timestamp' => String.valueOf(DateTime.now().getTime()/1000),
'oauth_token' => DeskSample.ACCESS_TOKEN,
'oauth_version' => '1.0'
};
String[] host = req.getEndpoint().split('\\?');
// parse get parameters
if (host.size() == 2) {
params.putAll(DeskSample.getParams(host[1]));
}
// parse body parameters
if (req.getBody() != null && req.getBody() != '') {
params.putAll(DeskSample.getParams(req.getBody()));
}
// create the base string
String baseString = '';
List<String> keyList = new List<String>(params.keySet());
keyList.sort();
for (String key : keyList) {
baseString += key + '=' + params.get(key) + '&';
}
baseString = req.getMethod().toUpperCase() + '&' +
EncodingUtil.urlEncode(host[0], 'UTF-8') + '&' +
EncodingUtil.urlEncode(baseString.substringBeforeLast('&'), 'UTF-8');
System.debug('BASE STRING: ' + baseString);
// create the signature
Blob sig = Crypto.generateMac('HmacSHA1', Blob.valueOf(baseString), Blob.valueOf(
DeskSample.OAUTH_SECRET + '&' + DeskSample.ACCESS_TOKEN_SECRET
));
String signature = EncodingUtil.urlEncode(EncodingUtil.base64encode(sig), 'UTF-8');
// create the header
String header = 'OAuth ';
for (String key : params.keySet()) {
header += key + '="' + params.get(key) + '", ';
}
header += 'oauth_signature="' + signature + '"';
// sign the request
System.debug('Authorization: ' + header);
req.setHeader('Authorization', header);
return req;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment