Skip to content

Instantly share code, notes, and snippets.

@mhamzas
Forked from tstachl/DeskSample.java
Created September 11, 2018 15:29
Show Gist options
  • Select an option

  • Save mhamzas/c8676dad2b16e47040678bff62edb1ad to your computer and use it in GitHub Desktop.

Select an option

Save mhamzas/c8676dad2b16e47040678bff62edb1ad to your computer and use it in GitHub Desktop.

Revisions

  1. @tstachl tstachl created this gist Apr 13, 2013.
    109 changes: 109 additions & 0 deletions DeskSample.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,109 @@
    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;
    }
    }