Sub topic 14
Client credentials flow: server-to-server access without a user login
This chapter assumes you know the connected app setup from earlier chapters, can read a token response, and can call the REST API with a bearer token. You also need a Salesforce org with My Domain enabled and administrator access.
What the flow does
The client credentials flow lets a server get an access token with only the consumer key and consumer secret. No user logs in, and no signed assertion is needed. Salesforce issues the token for a single run-as user that you designate on the connected app. Every API call made with that token runs with that user's permissions.
Compared with the JWT bearer flow, this flow is simpler. You have no key pair, no certificate, and no pre-authorization step. The trade-off is that the consumer secret is the only credential. Anyone who holds it can get a token.
Configure the connected app
In Setup, open the connected app (or create one) and set these options:
- Enable OAuth settings and add a callback URL. Salesforce requires one even though this flow never redirects.
- Select the scopes the integration needs, such as
api. - Check Enable Client Credentials Flow and accept the security warning.
- Open Manage, then Edit Policies. Under Client Credentials Flow, set Run As to the integration user.
- Copy the consumer key and consumer secret from Manage Consumer Details.
The run-as user should be a dedicated integration user with a permission set that grants only what the integration needs. Do not use a personal administrator account.
Request a token
Post to the token endpoint of your My Domain URL, not to login.salesforce.com.
curl -X POST https://MyDomainName.my.salesforce.com/services/oauth2/token \
-d grant_type=client_credentials \
-d client_id=<CONSUMER_KEY> \
-d client_secret=<CONSUMER_SECRET>The response contains access_token, instance_url, token_type, and scope. It does not include a refresh token. When the access token expires, repeat the same request.
Call the API with the returned token and instance_url:
curl https://MyDomainName.my.salesforce.com/services/data/v60.0/limits \
-H "Authorization: Bearer <ACCESS_TOKEN>"Common errors
| Error | Likely cause |
|---|---|
| `invalid_client_id` | Wrong consumer key, or the connected app is not available in this org. |
| `invalid_client` | Wrong consumer secret. |
| `unsupported_grant_type` | Client Credentials Flow is not enabled, or the request went to `login.salesforce.com`. |
| `invalid_grant` | No run-as user is set, or that user is inactive or lacks access to the app. |
When to choose it
Use this flow for back-end jobs where no person is involved and one fixed identity is acceptable. If you need Salesforce to record actions under a specific named user, or you want to avoid a shared secret, use the JWT bearer flow.
Exercise
A scheduled job gets unsupported_grant_type from https://login.salesforce.com/services/oauth2/token when it sends grant_type=client_credentials. Name two things to check.
Show answer
First, confirm that Enable Client Credentials Flow is checked on the connected app. Second, send the request to the org's My Domain URL (https://MyDomainName.my.salesforce.com/services/oauth2/token) instead of the generic login host. Also confirm that a run-as user is set in the app's policies.