Last active
August 29, 2015 14:25
-
-
Save adtennant/0f1252368f2439fe27ff to your computer and use it in GitHub Desktop.
Code snippets from: http://adtennant.co.uk/blog/2015/03/09/using-named-credentials-in-apex/
This file contains 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
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()); |
This file contains 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
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()); |
This file contains 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
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()); |
This file contains 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
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