Sub topic 2
Choosing an Authentication Protocol for Apex Callouts
Assume you know basic Apex, HTTP methods and headers, permission sets, and the difference between API keys and OAuth 2.0. This chapter focuses on choosing and using authentication settings, not on building an API.
Choose the authentication protocol
An External Credential defines how Salesforce authenticates to the external service. Select the protocol that matches the service:
| Protocol | Best fit | Credential ownership |
|---|---|---|
| OAuth 2.0 | A service that issues access tokens | Salesforce or each user |
| API key or custom header | A service requiring a fixed key | Salesforce |
| Basic authentication | A service requiring username and password | Salesforce |
Prefer OAuth 2.0 when the provider supports it. Use a custom authentication protocol for an API key or another required header. Keep secrets in Setup, not in Apex.
Principals: named versus per user
A principal connects an External Credential to a specific authentication identity.
- A named principal uses one shared identity. Every authorized user calls the external service as that identity.
- A per-user principal stores a separate token or identity for each Salesforce user. Each user must authorize access.
Use a named principal for shared integrations, such as a company weather account. Use per-user authentication when the external service must enforce each user's permissions or record ownership.
Grant access with permission sets
A user needs External Credential Principal Access in a permission set. Assign the permission set to users who should use the principal. Without this access, the callout can fail even when the endpoint and authentication settings are correct.
Call the service with `callout:`
The callout: prefix tells Salesforce to resolve the Named Credential and apply its authentication.
public with sharing class WeatherService {
public static String getForecast(String city) {
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:Weather_API/forecast?city=' +
EncodingUtil.urlEncode(city, 'UTF-8'));
req.setMethod('GET');
HttpResponse response = new Http().send(req);
if (response.getStatusCode() != 200) {
throw new CalloutException('Weather API returned ' +
response.getStatusCode());
}
return response.getBody();
}
}Test with `HttpCalloutMock`
Tests cannot make real HTTP requests. HttpCalloutMock returns a controlled response and can verify the generated request.
@IsTest
private class WeatherServiceTest {
private class MockResponse implements HttpCalloutMock {
public HttpResponse respond(HttpRequest request) {
System.assertEquals('GET', request.getMethod());
HttpResponse response = new HttpResponse();
response.setStatusCode(200);
response.setBody('{"temp":21}');
return response;
}
}
@IsTest
static void returnsForecast() {
Test.setMock(HttpCalloutMock.class, new MockResponse());
Test.startTest();
String body = WeatherService.getForecast('Oslo');
Test.stopTest();
System.assertEquals('{"temp":21}', body);
}
}Exercise
When should you choose a per-user principal instead of a named principal?
Show answer
Choose per-user authentication when the external service must identify and authorize each Salesforce user separately.