Sub topic 5
Choosing an OAuth Flow
This chapter assumes you can create a connected app, know its consumer key and callback URL, and can send HTTP requests with curl. It does not repeat how scopes work. It covers how to pick the flow that fits your integration.
What a flow decides
An OAuth flow is the sequence of requests that ends with an access token. Salesforce supports several flows. Your choice depends on two questions:
- Is a person present to log in during the exchange?
- Can the client keep a secret or a private key safely?
The answers also decide which user the token acts as. That user's profile and permission sets limit what the token can do.
The three flows in this tutorial
| Flow | Person present? | Runs as | Typical use |
|---|---|---|---|
| Authorization code with PKCE | Yes | The user who logs in | Web apps, mobile apps, single-page apps, CLI tools |
| JWT bearer | No | The user named in the `sub` claim | CI pipelines, scheduled server jobs |
| Client credentials | No | The run-as user set in the app's policies | Service-to-service calls with one fixed identity |
Authorization code with PKCE sends the user to Salesforce to log in. The app then redeems a one-time code. PKCE (Proof Key for Code Exchange) adds a verifier that only your client knows. That protects the code if someone intercepts it, so public clients that cannot hold a secret can use the flow safely.
The JWT bearer flow has no login screen. Your server signs a JWT with a private key. Salesforce checks the signature against the certificate uploaded to the app. The user must be pre-authorized, either by an admin or through a profile or permission set.
The client credentials flow sends only the consumer key and secret. Salesforce issues a token for the run-as user chosen in the app's policies. It is the simplest option, but every call shares one identity.
Decision path
Trade-offs to weigh
- Auditability. Authorization code records the real person. Client credentials records only the run-as user.
- Secret handling. Client credentials depends on a consumer secret. JWT bearer depends on a private key, and the key never leaves your side. Rotating a certificate is usually safer than rotating a shared secret.
- Setup effort. Client credentials takes the least work. JWT bearer needs a key pair, a certificate, and user pre-authorization.
- Least privilege. For non-interactive flows, use a dedicated integration user with only the permissions the job needs.
Common mistakes
- Choosing client credentials, then running it as a user with System Administrator rights.
- Using JWT bearer without pre-authorizing the user. The request fails with an
invalid_granterror. - Sending the JWT to the wrong audience. Use
https://login.salesforce.comfor production,https://test.salesforce.comfor sandboxes, or your My Domain URL.
Exercise
A nightly job on your build server must deploy metadata to a sandbox. No one is available to log in, and the audit log must show the same named integration user each time. Which flow fits, and why?
Show answer
JWT bearer. It needs no interactive login, and the sub claim names the integration user, so the audit log shows that user. Use https://test.salesforce.com or the sandbox My Domain URL as the audience.