Skip to content

Instantly share code, notes, and snippets.

@adtennant
Last active August 29, 2015 14:25
Show Gist options
  • Save adtennant/0f1252368f2439fe27ff to your computer and use it in GitHub Desktop.
Save adtennant/0f1252368f2439fe27ff to your computer and use it in GitHub Desktop.
HttpRequest req = new HttpRequest();
req.setEndpoint('https://api.example.com/path');
req.setMethod('GET');
String username = 'username';
String password = 'password';
Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);
Http http = new Http();
HttpResponse res = http.send(req);
System.debug(res.getBody());
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:HTTP_Basic_Credential/path');
req.setMethod('GET');
Http http = new Http();
HttpResponse res = http.send(req);
System.debug(res.getBody());
HttpRequest loginRequest = new HttpRequest();
loginRequest.setEndpoint('https://login.salesforce.com/services/oauth2/token');
loginRequest.setMethod('POST');
loginRequest.setHeader('Content-Type', 'application/x-www-form-urlencoded');
String clientId = 'client_id';
String clientSecret = 'client_secret';
String username = 'username';
String password = 'password';
String securityToken = 'security_token';
String body = 'grant_type=password&client_id=' + clientId + '&client_secret=' + clientSecret + '&username=' + username + '&password=' + password + securityToken;
loginRequest.setBody(body);
Http http = new Http();
HttpResponse loginResponse = http.send(loginRequest);
Map<String, String> loginResponseBody = (Map<String, String>)JSON.deserialize(loginResponse.getBody(), Map<String, String>.class);
HttpRequest feedRequest = new HttpRequest();
feedRequest.setEndpoint(loginResponseBody.get('instance_url') + '/services/data/v32.0/chatter/feeds/news/me/feed-elements');
feedRequest.setMethod('GET');
String authorizationHeader = 'Bearer ' + loginResponseBody.get('access_token');
feedRequest.setHeader('Authorization', authorizationHeader);
HttpResponse feedResponse = http.send(feedRequest);
System.debug(feedResponse.getBody());
HttpRequest feedRequest = new HttpRequest();
feedRequest.setEndpoint('callout:OAuth_Credential/services/data/v32.0/chatter/feeds/news/me/feed-elements');
feedRequest.setMethod('GET');
Http http = new Http();
HttpResponse feedResponse = http.send(feedRequest);
System.debug(feedResponse.getBody());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment