4

I have all four Oauth token consumerKey, consumerSecret, accessKey and accessSecret. I need to make a request to a url from my apex code. Is there any library or a way to make this request. In java I did this using SignPost and HttpClient libraries. Is there any signpost library equivalent in apex/salesforce ?

  • Look into the documentation for HTTPRequest/HTTP/HTTPResponse it will get you what you need – Eric Aug 5 '14 at 20:30
  • Anyone has a page reference library example which does the same? – kishore Aug 11 '14 at 17:06
  • Can you be more specific? What have you tried? Where are you stuck? What do you need to do? – Eric Aug 11 '14 at 18:45
  • I have used these 4 oauth tokens in java to make http rest request to a url. This is stackoverflow.com/questions/25146670/… how i used used them. I used signpost library. Its working. I need to write this code block in apex. I have also tried what keith suggested in answer. But I couldn't figure out the correct way to do it. – kishore Aug 11 '14 at 22:02
3

Apex has pretty good APIs that are the equivalent of a Java HttpClient. You need to check the URLs and parameter names carefully for various implementations of OATH and related APIs. Here's what getting the email address from Facebook after you've got the access token might look like:

public String accessTokenToEmail(String accessToken) {

     PageReference pr = new PageReference('https://graph.facebook.com/oauth/access_token');
     pr.getParameters().put('fields', 'email');
     pr.getParameters().put('access_token', accessToken);

     String body = httpGet(pr);
     return extractJsonField(body, 'email');
 }
 private String httpGet(PageReference pr) {

     HttpRequest req = new HttpRequest();
     req.setEndpoint(pr.getUrl());
     req.setMethod('GET');
     req.setTimeout(60 * 1000);

     HttpResponse res = new Http().send(req);
     return res.getBody();
}
private String extractJsonField(String body, String field) {

    JSONParser parser = JSON.createParser(body);
    while (parser.nextToken() != null) {
        if (parser.getCurrentToken() == JSONToken.FIELD_NAME
                && parser.getText() == field) {
            parser.nextToken();
            return parser.getText();
        }
    }
    return null;
}

You will also need to add a Remote Site Setting for each domain that you want to access e.g. "https://graph.facebook.com".

  • Can you please give an example on how to use oauth tokens in apex. In Java using signpost library, this is how we do OAuthConsumer consumer = new CommonsHttpOAuthConsumer(consumerKey,consumerSecret); consumer.setTokenWithSecret(accessToken,accessTokenSecret);HttpPost req= new HttpPost(url); consumer.sign(req);. I can't find a example of PageReference doing the same. Thanks!. – kishore Aug 8 '14 at 18:20
0

Here is the apex code how to get an access token for a Salesforce org:

HttpRequest request = new HttpRequest();

request.setEndpoint('https://test.salesforce.com/services/oauth2/token');  // sfdc sandbox
request.setMethod('POST');
request.setTimeout(60000);

String consumerKey = '...';
String consumerSecret  = '...';
String accessKey = '...';
String accessSecret = '...';

request.setBody(
    'grant_type=password' +
    '&client_id=' + EncodingUtil.urlEncode(consumerKey, 'UTF-8') +
    '&client_secret=' + EncodingUtil.urlEncode(consumerSecret, 'UTF-8') +
    '&username=' + EncodingUtil.urlEncode(accessKey, 'UTF-8') +
    '&password=' + EncodingUtil.urlEncode(accessSecret, 'UTF-8')
);

HttpResponse response = new Http().send(request);
System.debug(response.getBody());
  • unfortunately this didn't work. I think request body will be different. Getting 401 error. tokens are correct because same tokens are working at java code. – kishore Aug 12 '14 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.