Are you concerned about exposing the URL to external system for integration and wish not to do so? Are you worried about sharing the Username / Password to connect with external system and how to hide it? Then Named Credentials is the way to go and you are at right place to learn and know how to implement it!
Definition
A named credential specifies the URL of a callout endpoint and its required authentication parameters in one definition. To simplify the setup of authenticated callouts, specify a named credential as the callout endpoint.
If you instead specify a URL as the callout endpoint, you must register that URL in your org’s remote site settings and handle the authentication yourself.
For example, for an Apex callout, your code would need to handle authentication, which can be less secure and especially complicated for OAuth implementations.
Difference Between Authentication Provider & Named Credentials
Developers usually gets confused between Authentication Provider & Named Credentials. The difference is Authentication Provider is used for authenticating external application using OAuth where as Named Credentials is used to hide URL information from being exposed rather gives a short friendly name to use instead.
How to setup Named Credentials in Salesforce
Check how to Setup Named Credentials in Salesforce. I’ve highlighted with Screenshots as well.
How Named Credential Works?
– Direct way to make Callout 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://my_endpoint.example.com/some_path'); | |
req.setMethod('GET'); | |
// Because we didn't set the endpoint as a named credential, | |
// our code has to specify: | |
// – The required username and password to access the endpoint | |
// – The header and header information | |
String username = 'myname'; | |
String password = 'mypwd'; | |
Blob headerValue = Blob.valueOf(username + ':' + password); | |
String authorizationHeader = 'BASIC ' + | |
EncodingUtil.base64Encode(headerValue); | |
req.setHeader('Authorization', authorizationHeader); | |
// Create a new http object to send the request object | |
// A response object is generated as a result of the request | |
Http http = new Http(); | |
HTTPResponse res = http.send(req); | |
System.debug(res.getBody()); |
– Callout using Named Credentials
Syntax to use Named Credentials. In the following code snippet, we’re using SalesforceNC instead of ENDPOINT.
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 HTTP Request(); | |
feedRequest.setEndpoint('callout:SalesforceNC/services/data/v32.0'); | |
feedRequest.setMethod('GET'); | |
HTTP http = new HTTP(); | |
HTTPResponse feedResponse = http.send(feedRequest); | |
System.debug(feedResponse.getBody()); |
Is there any other way to send credentials to endpoint than to place to Username and password in authorization header?
LikeLike