Sub topic 3
Choosing and Securing the Run-As User
Assume you understand connected apps, OAuth token requests, HTTP headers, and Salesforce permission sets. The run-as user is the Salesforce user whose permissions apply to a client credentials integration.
Choose and secure the run-as user
The run-as user should be a dedicated integration user, not an administrator or employee account. Assign only the permission sets required by the integration. Review its object permissions, field permissions, sharing access, API access, and login restrictions.
A dedicated user makes audit logs easier to interpret and limits damage if the client secret is exposed.
Client credentials flow
Client credentials is a server-to-server flow. The application sends its connected app key and secret to the token endpoint on the org's My Domain URL. Salesforce returns an access token for the configured run-as user. No user login, callback URL, refresh token, or interactive consent is required.
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"Use the returned instance_url for API requests. The token has the run-as user's effective permissions.
Choose the flow
| Scenario | Suitable flow | Main requirement |
|---|---|---|
| A web application acts for a person | Web server flow | Redirect URI, state, and PKCE |
| A scheduled service needs a named Salesforce identity | JWT bearer | Certificate, private key, and pre-authorized user |
| A service always uses one fixed identity | Client credentials | Run-as user and connected app secret |
Use the web server flow when users must approve or select their own Salesforce identity. Use JWT when a server needs a named user without storing a password. Use client credentials when one fixed run-as identity is sufficient.
Revocation and troubleshooting
Revoke a token by posting it to Salesforce's revoke endpoint:
curl -X POST "https://login.salesforce.com/services/oauth2/revoke" \
-d "token=$ACCESS_TOKEN"An invalid_grant response often means an expired or reused authorization code, a bad JWT assertion, clock skew, or a revoked refresh token. invalid_client_id usually indicates the wrong consumer key or endpoint. redirect_uri_mismatch means the callback URL differs from the connected app configuration, including its scheme, host, path, or trailing slash.
Security practices
Store secrets and private keys in a secrets manager or protected environment variables. Never log tokens, authorization codes, client secrets, or JWT assertions. Use short-lived access tokens, narrow scopes, HTTPS, key rotation, and separate connected apps for separate environments.
Exercise
Which flow fits a nightly service that always uses one dedicated integration user?
Show answer
Client credentials, provided that the connected app has a least-privileged run-as user.