Sub topic 8
Client credentials flow and the run-as user
This chapter covers the client credentials flow in Salesforce. It explains how the flow picks the user an integration runs as, how to request a token, and which errors to expect. It assumes you can administer Salesforce (users, profiles, permission sets, Setup), read an HTTP request, and run curl from a terminal.
What the flow does
The client credentials flow is a server-to-server OAuth flow with no interactive login. The app sends its consumer key and consumer secret to the token endpoint. Salesforce returns an access token. No browser, no redirect, and no user password is involved.
Because no person signs in, Salesforce needs another way to decide whose permissions the token carries. That user is the run-as user. You set it in the app's policies, and every token issued through the flow acts as that user.
This has a direct consequence for security. The token can do anything the run-as user can do, within the app's scopes. Choosing and restricting that user is the most important decision in this flow.
How the flow works
The flow has no refresh token. When the access token expires, the app requests a new one with the same credentials. This is why the secret must be stored carefully: it is the only thing standing between an attacker and a valid token.
Set up the app
Do these steps in the connected app or external client app that the integration uses.
- In the OAuth settings, enable OAuth and add the scopes the integration needs, such as
api. A callback URL is still required by the form even though this flow never uses it. - Select Enable Client Credentials Flow. Salesforce shows a warning that the flow lets the app act as the run-as user. Read it before you accept.
- Save the app and copy the consumer key and consumer secret.
- In the app's policies, set Permitted Users to Admin approved users are pre-authorized. Then assign the profile or permission set that contains the run-as user.
- In the client credentials section of the policies, choose the Run As user.
Step 4 and step 5 are separate settings. The first controls who can use the app. The second controls whose identity the client credentials token takes. Missing either one causes failures.
Choose the run-as user carefully
Create a dedicated integration user for each integration. Do not reuse an administrator or a person's account. A dedicated user gives you three things:
- Least privilege. Grant only the permission sets the integration needs. A shared administrator account turns a leaked secret into full org access.
- Clear audit trails. Records and logs show the integration user as the actor, so you can tell automated changes from human ones.
- Stability. The integration keeps working when employees leave or change roles.
A typical integration user has the Salesforce Integration user license, or an API-only profile, and a permission set that grants object and field access for that one job. Remember that the user's permissions are the ceiling. Scopes can narrow what the token does, but they cannot grant more than the user has.
If the run-as user is frozen or deactivated, token requests fail. Treat that user as part of the integration's configuration and monitor it.
Request an access token
Send a POST request to your My Domain token endpoint. This flow requires My Domain, so use it rather than the generic login URL.
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"A successful response is JSON. It contains these fields:
| Field | Meaning |
|---|---|
| `access_token` | The bearer token to send on API calls |
| `instance_url` | The base URL for API requests |
| `token_type` | Always `Bearer` |
| `scope` | The scopes granted to the token |
| `issued_at` | Time of issue, in milliseconds since the Unix epoch |
Note that there is no refresh_token field. The session lifetime follows the session settings of the app and the org.
Verify the token
Call a simple resource to confirm the token works and runs as the right user.
curl https://MyDomain.my.salesforce.com/services/data/ \
-H "Authorization: Bearer $ACCESS_TOKEN"A list of API versions confirms that the token is valid. To confirm identity, call the /services/oauth2/userinfo endpoint with the same header. The response should show the integration user, not a person.
Compare with the other server flows
| Aspect | Client credentials | JWT bearer |
|---|---|---|
| Proof of identity | Consumer key and secret | Signed JWT using a certificate |
| Who it acts as | One fixed run-as user | The user named in the `sub` claim |
| Secret to protect | Consumer secret | Private key |
| Setup effort | Low | Higher (certificate and key) |
| Fits | A single service with one identity | Several users, or stricter key-based security |
Use client credentials when one service always acts as one identity and you want simple setup. Choose JWT bearer when you need different users per request, or when you prefer a private key that never leaves your server over a shared secret sent on each request.
Common errors
invalid_grantwith "no client credentials user enabled". No run-as user is set in the policies. Set one and retry.unsupported_grant_type. The flow is not enabled on the app, or the request went to a URL that does not support it. Check the checkbox and use the My Domain URL.invalid_clientorinvalid_client_id. The consumer key or secret is wrong, or the app has not finished propagating after a change. Wait a few minutes and compare the values.INVALID_SESSION_IDon API calls. The token expired. Request a new one.- Permission errors on API calls. The run-as user lacks object or field access. Fix the permission sets, not the app.
For deeper diagnosis, see the troubleshooting chapter.
Exercise
Exercise
An integration using the client credentials flow gets invalid_grant on every token request. The app has the flow enabled and the consumer key and secret are correct. Name the setting you check first, and explain why the user's profile does not fix the problem on its own.
Show answer
Check that a Run As user is set in the client credentials section of the app's policies. The flow has no interactive login, so Salesforce can only issue a token if the app names the user it runs as. Assigning a profile or permission set only controls who is permitted to use the app. It does not choose the identity the token takes.
Exercise
A security reviewer finds that an integration's run-as user is a system administrator. What risk does this create, and what should replace it?
Show answer
Anyone who obtains the consumer secret gets a token with administrator-level access, limited only by the app's scopes. Replace it with a dedicated integration user that has only the permission sets the integration needs, so a leaked secret exposes far less.