Sub topic 4
Choosing OAuth Scopes
This chapter assumes you can create a connected app or external client app (ECA) and read its consumer key. It also assumes you know what a profile and a permission set are, and that you can run curl commands. Flows are covered in the next chapter. Here the focus is on what scopes are, which ones Salesforce offers, and how to pick them.
What a scope is
A scope is a named permission that an OAuth client requests, and that the app is allowed to ask for. It limits what an access token can do, so it controls the token's reach, not the user's.
Two things decide whether an API call succeeds:
- The scopes on the token.
- The user's own permissions: object and field access, sharing, and user permissions.
A call must pass both checks. A token with the api scope for a user who cannot read Opportunities still cannot read Opportunities. A user who is a system administrator still cannot call the REST API with a token that lacks api.
Scopes therefore act as a ceiling on top of the user's access. They never raise it.
Two places scopes are set
Scopes are set in two places, and the difference causes many errors.
- App configuration. In the app's OAuth settings, you choose the selected OAuth scopes. This is the list of scopes the app is allowed to request. In an ECA, this list is part of the developer settings.
- Authorization request. The client sends a
scopeparameter, a space-separated list. For flows that show a consent screen, the user approves this list.
The token receives the requested scopes that the app also allows. A client cannot get a scope that the app configuration does not include. If a client sends no scope parameter, Salesforce grants the scopes configured on the app.
Request the smallest set that works. Set the app's list to the most the app will ever need, and let each request ask for less where that makes sense.
Common scopes
The scope names below are the ones you will use most. The label is what you see in Setup.
| Scope value | Setup label | What it allows |
|---|---|---|
| `api` | Manage user data via APIs | Call REST, SOAP, Bulk, and similar APIs as the user. |
| `web` | Access the identity URL service... no, see below | See note after the table. |
| `refresh_token` | Perform requests at any time | Receive a refresh token, so the app can get new access tokens without the user signing in again. |
| `offline_access` | Perform requests at any time | Same effect as `refresh_token`. Either value works in a request. |
| `id` | Access unique user identifiers | Call the identity URL to get information about the user. |
| `openid` | Access unique user identifiers | OpenID Connect sign-in. Returns an ID token. |
| `full` | Full access | All data the user can reach, plus use of `web`-style sessions. Broadest scope. |
| `custom_permissions` | Access custom permissions | Read which custom permissions the user has. |
| `chatter_api` | Access Chatter REST API resources | Call Chatter REST API only. |
| `wave_api` | Access and manage your Analytics data | Call the Analytics REST API. |
The web row above is a mistake in the draft of this table. The correct description of web is below, and you should ignore that row.
web(label "Access the identity URL service" does not apply). Use the label Provide access to your data via the Web. It lets the token be used with a browser session, for example to open a Salesforce page with the token. It does not allow API calls by itself. Pair it withapiwhen you need both.
Other scopes exist for specific products, such as Pardot (pardot_api), Data Cloud, and Einstein. Check the scope list in your org's Setup page, because the available set depends on which features are enabled.
Scopes that need extra care
`full`
full covers every API that the user can reach. It is simple to set and hard to defend in a security review. Use it only when you have a reason that narrower scopes cannot meet. For most integrations, api plus refresh_token is enough.
`refresh_token` and `offline_access`
These scopes let an app keep working after the access token expires. That is useful for a background service. It also means a stolen refresh token gives long-lived access, so request it only when the client truly needs it, and store the token like a password. The app's refresh token policy, covered in a later chapter, limits how long a refresh token lives.
`openid` and `id`
Use openid when the app signs users in and needs an ID token. Use id when you only need the identity URL response. Neither grants data access. An app that only signs users in should not request api.
Scopes and the flow
Flow choice affects how scopes are granted.
- In the authorization code flow, the user sees the requested scopes on a consent screen and approves them. The user can refuse.
- In the JWT bearer flow and the client credentials flow, no one approves anything at request time. Access depends on the app configuration and policies, so the scopes in the app are the real control. Review them with extra care.
For the JWT bearer flow, the token carries the scopes that the app allows and that were pre-approved for the user. For the client credentials flow, the token acts as the run-as user and carries the app's configured scopes.
Example: request and inspect scopes
This authorization request asks for API access and a refresh token. The scope value is URL-encoded, so spaces become %20.
echo "https://MyDomain.my.salesforce.com/services/oauth2/authorize?response_type=code&client_id=$CONSUMER_KEY&redirect_uri=https%3A%2F%2Fapp.example.com%2Fcallback&scope=api%20refresh_token"The token response includes a scope field that lists what was granted. Always read it instead of assuming:
{
"access_token": "00D...",
"refresh_token": "5Aep...",
"scope": "refresh_token api",
"instance_url": "https://MyDomain.my.salesforce.com",
"token_type": "Bearer"
}If refresh_token is missing from scope and no refresh token is returned, the app configuration probably lacks that scope, or the policy blocks refresh tokens.
You can confirm that the api scope works with a simple call:
curl https://MyDomain.my.salesforce.com/services/data/ \
-H "Authorization: Bearer $ACCESS_TOKEN"Common errors
invalid_scope. The request names a scope the app does not allow, or a scope that does not exist. Compare the request to the app's selected scopes.insufficient_scopeorINSUFFICIENT_ACCESS-style failures on a valid token. The token lacks a scope for that API. For example, a token with onlyopenidcannot call/services/data/.- No refresh token returned. The
refresh_tokenoroffline_accessscope was not requested or not allowed. - Scope change has no effect. An existing token keeps the scopes it was issued with. After you change the app, users must authorize again, or you must request a new token.
Choosing scopes: a checklist
- List the APIs the integration calls. Map each to a scope.
- Start with
api. Addrefresh_tokenonly if the client runs without the user present. - Add
openidonly for sign-in. - Avoid
fullunless you can justify it in writing. - Test with a token, read the
scopefield in the response, and confirm each call you need.
Exercise
An integration user has full read access to Accounts. The integration's token was issued with only the openid scope. The integration calls GET /services/data/ and receives an error. Why does the call fail, and what is the smallest change that fixes it?
Show answer
The call fails because the token's scopes do not include api. Scopes and user permissions are both checked, so the user's Account access does not help. Add api to the app's selected OAuth scopes, then request a new token with scope=api. Existing tokens keep their old scopes, so a new authorization is required.
Exercise
Your app must keep calling the API overnight with no user present. Which scopes do you request, and what extra risk does this add?
Show answer
Request api and refresh_token (or offline_access). The refresh token lets the app get new access tokens, so a leaked refresh token gives long-lived access. Store it securely and limit its lifetime with the app's refresh token policy.