Sub topic 4
How the Client Credentials Flow and Run-As User Work
This chapter assumes you know what an OAuth 2.0 access token is, can read short Python and curl examples, and have an External Client App with scopes configured. Here you will see what the client credentials flow does, which Salesforce user the agent acts as, and why that matters.
What the flow does
The client credentials flow is an OAuth 2.0 flow for server-to-server calls. No person signs in and no browser redirect happens. Your system proves its identity with a client ID and a client secret, and the org returns an access token.
Because no person is involved, Salesforce has no user to attach the token to. You must name one in advance. That user is the run-as user.
The run-as user
The run-as user is the Salesforce user that the External Client App acts as when it uses the client credentials flow. You set it in the app's OAuth policies, in the client credentials flow settings. Every call made with the token runs with that user's identity.
This has three practical effects:
- Permissions. The token can reach only what the run-as user can reach, and only within the scopes granted to the app. Both limits apply.
- Data access. Records and fields the agent reads or changes on a conversation's behalf follow the run-as user's access.
- Audit. Activity is recorded against that user, not against the end customer who is typing in your application.
Use a dedicated integration user for this role. Do not use a named administrator. A dedicated user has a clear audit trail and can be given the minimum permissions needed. It also keeps the integration running if a person leaves the company.
Making the token request
The request goes to your org's My Domain URL, not to a generic login host. The body is form-encoded, not JSON.
import os
import requests
MY_DOMAIN = os.environ["SF_MY_DOMAIN"] # for example https://mycompany.my.salesforce.com
def get_access_token() -> str:
response = requests.post(
f"{MY_DOMAIN}/services/oauth2/token",
data={
"grant_type": "client_credentials",
"client_id": os.environ["SF_CLIENT_ID"],
"client_secret": os.environ["SF_CLIENT_SECRET"],
},
timeout=30,
)
response.raise_for_status()
return response.json()["access_token"]The function reads all three values from environment variables, so no secret appears in source code. The data= argument makes requests send form-encoded fields, which is what the token endpoint expects.
The client ID and secret in the External Client App are called the consumer key and consumer secret in its settings. They are the same values.
Common errors
| Symptom | Likely cause |
|---|---|
| `invalid_client_id` or `invalid_client` | Wrong consumer key or secret, or the app is not available in this org |
| `unsupported_grant_type` | The client credentials flow is not enabled on the app |
| `invalid_grant` or a message about no run-as user | No run-as user is set, or that user is inactive or frozen |
| 404 or an HTML login page | The request went to the wrong host instead of your My Domain |
| Token works but later calls return 403 | The run-as user or the scopes lack permission for the agent |
If the token request succeeds but the agent is not reachable, check the run-as user's permissions before you change anything else.
Check your understanding
Exercise
Your token request returns an access token, but the agent call that follows fails with a permission error. The scopes on the External Client App look correct. What should you check next, and why?
Show answer
Check the run-as user's permissions. A token is limited by both the app's scopes and the run-as user's own access, so a correct scope list does not help if the run-as user lacks access to the agent or related data. Also confirm the user is active.
Exercise
Why should the run-as user be a dedicated integration user and not a named administrator?
Show answer
Activity is recorded against the run-as user, so a dedicated user gives a clear audit trail. It can also be limited to the minimum permissions the integration needs, and it is not tied to one person's employment or role changes.