Sub topic 1
OAuth 2.0 Fundamentals in Salesforce
Prerequisites
This chapter assumes you can do the following:
- Navigate Setup and understand users, profiles, and permission sets.
- Send HTTP requests with curl and read JSON responses.
- Tell authentication (who you are) from authorization (what you may do).
The working model
OAuth 2.0 lets an external application call Salesforce APIs on behalf of a user, or as a designated user, without ever holding that user's password. Four roles take part:
| Role | In Salesforce |
|---|---|
| Resource owner | The user whose data is accessed |
| Client | Your external application |
| Authorization server | Salesforce, at your My Domain URL |
| Resource server | The Salesforce APIs, such as REST |
The client proves who it is with a consumer key (and sometimes a secret or signed assertion). Salesforce checks the user and the app's configuration, then returns an access token. The client sends that token with each API call.
Tokens you will meet
- Access token: A short-lived credential sent in the
Authorization: Bearerheader. Its lifetime follows the session timeout settings. - Refresh token: A long-lived credential that obtains new access tokens. It is only issued when the app requests the
refresh_tokenscope and the flow supports it. - Authorization code: A one-time value exchanged for tokens in the authorization code flow.
Where the configuration lives
Salesforce does not let arbitrary software request tokens. The client must be registered as an app inside the org. Today that registration is either a connected app or the newer external client app (ECA). The registration holds the consumer key, the allowed callback URLs, the scopes, and the policies. The next chapters cover each in detail.
Verifying a token
Once you hold an access token, test it by calling the REST API versions resource:
curl https://MyDomain.my.salesforce.com/services/data/ \
-H "Authorization: Bearer $ACCESS_TOKEN"A JSON list of API versions means the token is valid. An error response is useful information too.
Common errors at this stage
- Wrong host: Token requests go to your My Domain URL. A sandbox needs its own sandbox domain, not the production one.
- Token in the wrong place: Sending the token as a query parameter instead of the
Authorizationheader fails. - Sharing one token across systems: Each integration should have its own app registration, so you can revoke or limit it separately.
- Treating a token as a user check: A valid token shows the app was authorized. What the call can do is still limited by the user's profile and permission sets.
Exercise
A client calls /services/data/ with a valid access token, but a later query on Account returns an insufficient-access error. Which two things limit what the call can do?
Show answer
The scopes granted to the token (for example, api) and the permissions of the user the token represents, set through profiles and permission sets. A valid token only proves the app was authorized.