Sub topic 8
Client credentials flow and the run-as user
This chapter assumes you can create a connected app or external client app with OAuth enabled, you know what a consumer key and secret are, and you can send a request with curl. You should also know how profiles and permission sets grant access.
What the flow does
The client credentials flow gives a server an access token without any person logging in. There is no browser, no redirect, and no callback. The app sends its consumer key and consumer secret to the token endpoint. Salesforce returns an access token.
Salesforce does not know which user the request is for, so you must name one in the app's policies. This is the run-as user. Every API call made with the token runs with that user's permissions, and records show that user as the actor.
Set up the app
- Enable OAuth settings and add at least one scope, such as
api. - Enable the client credentials flow in the OAuth settings. In a connected app this is "Enable Client Credentials Flow". Salesforce asks you to confirm because the flow uses only a secret.
- In the app's policies, set the run-as user to a dedicated integration user.
- Copy the consumer key and consumer secret.
Callback URLs are still required by the settings form in some app types. The flow never uses them.
Request a token
curl -X POST https://MyDomain.my.salesforce.com/services/oauth2/token \
-d grant_type=client_credentials \
-d client_id="$CONSUMER_KEY" \
-d client_secret="$CONSUMER_SECRET"Send the request to your My Domain URL. The generic login URL does not know which org owns the app. The response contains access_token, instance_url, and token_type. It does not contain a refresh token. When the token expires, request a new one.
Verify the token:
curl https://MyDomain.my.salesforce.com/services/data/ \
-H "Authorization: Bearer $ACCESS_TOKEN"Choose the run-as user carefully
The token can do anything the run-as user can do, within the granted scopes. Follow these rules:
- Use a dedicated integration user, not an administrator or a person's account.
- Give that user a profile or permission set with only the objects and fields the integration needs.
- Do not assign a user who will be deactivated or frozen. The flow fails when that happens.
- Treat the consumer secret like a password. Anyone who holds it can act as the run-as user.
Common errors
| Error | Likely cause |
|---|---|
| `unsupported_grant_type` | The client credentials flow is not enabled on the app. |
| `invalid_client_credentials` | Wrong key or secret, or the request went to the wrong domain. |
| `inactive user` | The run-as user is deactivated or has no valid license. |
| `no client credentials user enabled` | No run-as user is set in the policies. |
Exercise
A server gets an access token with the client credentials flow, but API calls fail with insufficient access to Account records. The app has the api scope. What should you check, and why?
Show answer
Check the permissions of the run-as user. The token acts as that user, so object and field access come from the user's profile and permission sets. The scope only allows API use. It does not grant access to records.