Sub topic 11
Setting up OAuth 2.0 with an authentication provider
This chapter assumes you can already create a Named Credential and an External Credential, and that you know the OAuth 2.0 vocabulary: client ID, client secret, scope, access token, refresh token.
What the authentication provider does
An API key is a fixed secret. OAuth 2.0 is different, because Salesforce must obtain an access token from the external system and renew it when it expires. For the Browser Flow, an authentication provider (auth provider) holds the OAuth client settings that Salesforce uses to talk to the external system's authorization server. The External Credential then points at that auth provider.
The auth provider stores:
- The consumer key and consumer secret issued by the external system.
- The authorize endpoint URL and the token endpoint URL.
- Default scopes.
It also generates a callback URL. You register that URL in the external system, so the system knows where to send the user after they approve access.
Setup order
The order matters, because each step needs a value from the previous one.
- In the external system, register an OAuth client. You can't finish this yet, because it asks for a redirect URI that Salesforce hasn't generated.
- In Salesforce Setup, open Auth. Providers and create one. Choose the provider type (use Open ID Connect for a generic server). Enter the consumer key, consumer secret, and the two endpoint URLs.
- Save, then copy the Callback URL that Salesforce generates.
- Paste the callback URL into the external system's OAuth client as an allowed redirect URI.
- Create the External Credential with protocol OAuth 2.0, flow type Browser Flow, your auth provider, and the scopes you need.
- Add a principal, create the Named Credential that references the External Credential, and grant principal access through a permission set.
The External Credential outline
Auth Provider: Weather_OIDC
Provider Type: Open ID Connect
Consumer Key: <from external system>
Consumer Secret: <from external system>
Authorize Endpoint URL: https://auth.example.com/oauth2/authorize
Token Endpoint URL: https://auth.example.com/oauth2/token
Default Scopes: forecast.read offline_access
External Credential: Weather_OAuth_EC
Authentication Protocol: OAuth 2.0
Authentication Flow Type: Browser Flow
Authentication Provider: Weather_OIDC
Scope: forecast.read offline_access
Principal: Weather_Principal (Named Principal)Request a refresh-token scope such as offline_access if the external system requires it. Without one, the connection stops working when the first access token expires.
Authenticating the principal
Creating the principal does not give it tokens. For a named principal, an admin opens the External Credential, chooses Authenticate on the principal's row, and signs in at the external system. Salesforce then stores the tokens, and every user with principal access calls the API as that one identity. Apex code stays the same as for any other credential, using callout: plus the Named Credential name.
Other flow types exist, such as Client Credentials and JWT Bearer. They skip the interactive sign-in and don't use the auth provider in the same way.
Common mistakes
- Redirect URI mismatch. The external system must contain the exact callback URL, including the domain Salesforce generated.
- Missing scopes. Tokens arrive but calls return 403, because the scope was not requested in the External Credential.
- Skipping Authenticate. Callouts fail because no token exists yet.
Exercise
You created an auth provider and an External Credential with a named principal, and assigned the permission set. Callouts still fail with an authentication error. What step was probably missed, and who performs it?
Show answer
The principal was never authenticated. An admin must use Authenticate on the named principal in the External Credential and sign in to the external system, so Salesforce can store the access and refresh tokens.