Sub topic 1
OAuth 2.0 fundamentals in Salesforce
OAuth 2.0 is the standard Salesforce uses to let outside software call its APIs without handling a user's password. This chapter covers the roles, endpoints, tokens, and failure modes that every later chapter builds on. Connected apps and external client apps both configure this same protocol.
Prerequisites
This chapter assumes you have:
- Basic Salesforce administration skills: users, profiles, permission sets, and navigating Setup.
- Familiarity with REST APIs and HTTP requests.
- A working idea of the difference between authentication (who you are) and authorization (what you may do).
- Basic command-line use, including running
curl.
Authentication versus delegated authorization
OAuth 2.0 is a framework for delegated authorization. A user grants an application limited access to their data, and the application receives a token instead of a password. The token carries the permissions the user approved, and it can be revoked without changing the password.
Salesforce also uses OpenID Connect, a thin identity layer on top of OAuth. It adds an ID token and a userinfo endpoint so an app can learn who signed in. You request it with the openid scope.
The four roles
Every OAuth exchange involves four parties. In Salesforce they map like this:
| OAuth role | Meaning | In Salesforce |
|---|---|---|
| Resource owner | The party who owns the data and grants access | A Salesforce user, or an integration user |
| Client | The application asking for access | Your app, defined by a connected app or external client app |
| Authorization server | Authenticates the user and issues tokens | Your org's `/services/oauth2/` endpoints |
| Resource server | Serves the protected data | The REST, SOAP, Bulk, and other APIs in your org |
In Salesforce the authorization server and resource server are the same org. The client is the only role you build.
Endpoints
Salesforce exposes OAuth endpoints under /services/oauth2/ on your org's domain.
| Endpoint | Purpose |
|---|---|
| `/services/oauth2/authorize` | Sends the user to log in and approve access |
| `/services/oauth2/token` | Exchanges a grant for tokens, or refreshes them |
| `/services/oauth2/revoke` | Invalidates an access or refresh token |
| `/services/oauth2/userinfo` | Returns identity details for the token's user |
The host you call matters. Use your My Domain URL, such as https://MyDomain.my.salesforce.com, for production work. Use https://login.salesforce.com only where a flow allows it, and https://test.salesforce.com for sandboxes that don't use My Domain. A request sent to the wrong host often fails with a confusing error, even when your credentials are correct.
Tokens
A token endpoint call returns some or all of the following, depending on the flow and the scopes you were granted.
- Access token. A short-lived credential sent with each API call. In Salesforce it is tied to a session, so session timeout settings affect how long it stays valid.
- Refresh token. A long-lived credential used only at the token endpoint to get a new access token. It is issued only if the app requested the
refresh_tokenscope (oroffline_access) and the flow supports it. - ID token. A signed JWT describing the user. It is issued only when the
openidscope is granted.
A typical token response looks like this:
{
"access_token": "00D...!AQ...",
"refresh_token": "5Aep...",
"signature": "base64-signature",
"scope": "refresh_token api",
"instance_url": "https://MyDomain.my.salesforce.com",
"id": "https://login.salesforce.com/id/00Dxx0000000000AAA/005xx000000000AAA",
"token_type": "Bearer",
"issued_at": "1767225600000"
}Always use the returned instance_url as the base for API calls instead of hard-coding a host. Treat the access token as an opaque string. Its format can change, so don't parse it.
Using an access token
The client sends the access token in an Authorization: Bearer header. This call lists the REST API versions and is a quick way to confirm a token works:
curl https://MyDomain.my.salesforce.com/services/data/ \
-H "Authorization: Bearer $ACCESS_TOKEN"A valid token returns a JSON list of API versions. An expired or revoked token returns HTTP 401 with an INVALID_SESSION_ID error. At that point the client should use its refresh token, or run its flow again if it has none.
How the pieces fit together
The sequence below shows the common case, a user-facing app that uses a refresh token.
Not every flow involves a user. The JWT bearer and client credentials flows skip the redirect and run as a pre-set user. Later chapters cover each flow. They all end the same way, with an access token used as a bearer credential.
What Salesforce checks on each request
Salesforce does not trust a token alone. A request must pass several checks, and a failure at any layer blocks it.
- The client must be registered, identified by its consumer key.
- The redirect (callback) URL must match one registered for the app.
- The scopes requested must be allowed by the app's configuration.
- The user must be allowed to use the app, based on its policies.
- The user's own permissions still apply. A token with the
apiscope cannot read an object the user cannot read.
The last point is the one people most often forget. Scopes limit what the app may do, and user permissions limit what the user may do. The effective access is the overlap of the two.
Common mistakes
- Treating the consumer secret as safe in a mobile app or browser code. Public clients cannot keep secrets, which is why PKCE exists.
- Hard-coding
login.salesforce.comwhen the org enforces its My Domain login. - Requesting scopes the app is not configured to allow, which produces an
invalid_scopeor a blocked consent screen. - Granting an integration the
fullscope whenapiis enough. - Storing refresh tokens in logs, source control, or plain-text config.
Where configuration lives
You do not configure OAuth at the org level. You configure it per client application, using a connected app or an external client app. That definition holds the consumer key, the callback URLs, the allowed scopes, and the policies that decide who may use the app. The next chapters cover connected apps, then external client apps, which Salesforce recommends for new integrations.
Exercise
A client calls the REST API with an access token that has the api scope. The call returns "insufficient access rights" on the Account object. Which layer is blocking the request, and why does the scope not fix it?
Show answer
The user's own permissions block it. The scope only limits what the app may do. The user's profile and permission sets still decide which objects the user can read, so the effective access is the overlap of the two. Fix it by granting the user (or the integration user) access to Account, not by adding scopes.
Exercise
Your app receives an HTTP 401 with INVALIDSESSIONID from the REST API. Name two ways to recover, depending on what the app holds.
Show answer
If the app has a refresh token, it posts it to the token endpoint with granttype=refreshtoken and gets a new access token. If it has no refresh token, it must run its OAuth flow again to obtain a new access token.