Sub topic 2
Authenticating with OAuth 2.0
This chapter assumes you know basic HTTP and JSON, and that you have seen Salesforce objects and records. Every Salesforce API call needs an access token. This chapter shows how an external client gets one, and which OAuth 2.0 flow fits which scenario.
The working model
OAuth 2.0 lets a client call Salesforce without holding a user's password. The client sends credentials to the Salesforce token endpoint and receives an access token. It then sends that token in an Authorization: Bearer header on each API call.
Three pieces are involved:
- Connected app (or external client app): the registration inside Salesforce that defines the client. It issues a client ID and a client secret, and it lists the allowed OAuth scopes and the callback URL.
- Scopes: the permissions the token carries, such as
apiorrefresh_token. - Token endpoint:
https://MyDomain.my.salesforce.com/services/oauth2/token. Use your My Domain URL, notlogin.salesforce.com, for production integrations.
Choosing a flow
| Scenario | Flow | Notes |
|---|---|---|
| Server-to-server, no user present | Client credentials | Runs as one designated "run as" user |
| Server-to-server with signed proof | JWT bearer | Needs a certificate uploaded to the app |
| Web app acting for a user | Authorization code (with PKCE) | User logs in and approves |
| Device with no browser | Device flow | User approves on a second device |
The older username-password flow still exists, but avoid it. It exposes credentials to the client and is blocked by default in new orgs.
Client credentials flow
For a backend integration, client credentials is the simplest. In the connected app, enable the flow and choose the "run as" user. The API calls then run with that user's permissions.
curl -X POST "https://MyDomain.my.salesforce.com/services/oauth2/token" \
-d "grant_type=client_credentials" \
-d "client_id=$CLIENT_ID" \
-d "client_secret=$CLIENT_SECRET"The response is JSON. It contains access_token, instance_url, token_type, and scope. Send the token on later calls:
curl "https://MyDomain.my.salesforce.com/services/data/v60.0/limits" \
-H "Authorization: Bearer $ACCESS_TOKEN"Use instance_url from the response as the base for later requests instead of hard-coding it.
Keeping tokens safe
- Store the client secret in an environment variable or secret manager. Never commit it to source control or write it to logs.
- Give the "run as" user a dedicated integration profile or permission set with only the access the integration needs.
- Cache the access token until it expires, then request a new one. Requesting a token on every call wastes time and counts against login limits.
- In user-based flows, use the refresh token to get new access tokens. Treat it with the same care as a password.
Common errors
| Error | Likely cause |
|---|---|
| `invalid_client_id` | Wrong client ID, or the app is not available in this org |
| `invalid_client` | Wrong secret, or the flow is not enabled on the app |
| `invalid_grant` | Expired or reused authorization code, bad JWT, or an inactive user |
| `unsupported_grant_type` | Flow not enabled, or the wrong endpoint domain |
| `INVALID_SESSION_ID` (HTTP 401) | Expired or revoked access token. Request a new one |
Exercise
A nightly job syncs data from a billing system into Salesforce. No person is logged in when it runs. Which flow fits best, and why must the job not request a new token for every API call?
Show answer
Client credentials (or JWT bearer if you need certificate-signed proof) fits, because no user is present to approve a login. The job should cache the token until it expires, because each token request adds latency and counts toward login limits.
Exercise
Your call returns HTTP 401 with INVALIDSESSIONID after working an hour earlier. What do you do?
Show answer
The access token has expired or was revoked. Request a new token from the token endpoint (or use the refresh token in a user-based flow), then retry the call.