Sub topic 4
Choosing OAuth scopes
This chapter assumes you can read an authorization URL and a token request, and that you have a connected app in a Salesforce org. It covers how to decide which scopes to put on the app and which to request at runtime.
What a scope does
A scope is a label that limits what an access token may do. The connected app lists the scopes it is allowed to use. The client then asks for some of them. The access token carries the ones that were granted.
Scopes narrow a token. They never widen it. A token with the api scope can call the REST API only as far as its user's profile, permission sets, sharing rules, and field-level security allow. Granting full to an integration user who can't read Contacts does not make Contacts readable.
The scopes you will use most
| Scope | What it allows | Typical use |
|---|---|---|
| `api` | REST, SOAP, Bulk, and other data APIs as the user | Almost every integration |
| `refresh_token` | Issues a refresh token (`offline_access` is the equivalent) | Web server flow apps that must work after the access token expires |
| `openid` | Returns an ID token and allows the identity URL | Single sign-on and "who is this user" lookups |
| `id`, `profile`, `email` | Identity details about the user | Showing a name or email in your app |
| `web` | Using the access token in a browser session, such as frontdoor URLs | Opening Salesforce UI from your app |
| `full` | Everything the user can access, except issuing refresh tokens | Rarely justified |
| `custom_permissions` | Reading which custom permissions the user has | Feature gating in your app |
Other scopes exist for specific products, such as Chatter, Tableau CRM, and Data Cloud. Add them only when you call those APIs.
Two details that surprise people
full does not include refresh_token. If you select full and expect a refresh token, you will not get one. Request refresh_token separately.
api does not give browser access. A token with only api can call REST endpoints but cannot log a user into the Salesforce UI. That needs web.
Scopes by flow
The flow decides which scopes make sense.
- Web server flow: the client sends a
scopeparameter on the authorization request. Addrefresh_tokenif the app must renew its own access. Addopenidif you want an ID token. - JWT bearer flow: the assertion has no
scopeclaim. The token gets the scopes selected on the connected app. A refresh token is not useful here, because the server can sign a new assertion whenever it needs one. - Client credentials flow: the token gets the scopes selected on the connected app. There is no user login and no refresh token. Request a new token when the old one expires.
For JWT bearer and client credentials, the scope list in the connected app is the only place that controls scope. For the web server flow, you control it in two places, the connected app and the request.
Requesting scopes in the web server flow
Scopes are space-separated in the scope parameter. In a URL, a space is encoded as %20 (or +).
https://login.salesforce.com/services/oauth2/authorize?response_type=code&client_id=<CONSUMER_KEY>&redirect_uri=https%3A%2F%2Fapp.example.com%2Foauth%2Fcallback&scope=api%20refresh_token&state=<RANDOM_STATE>Here the app asks for api and refresh_token. The consent screen shows the user a plain-language description of each scope, so every extra scope is something the user has to read and approve.
Requesting a scope that the connected app has not enabled fails. The authorization endpoint returns an invalid_scope error, not a token with fewer scopes. Fix it by adding the scope to the connected app, or by removing it from the request.
Verify what you were granted
Do not assume the token has the scopes you asked for. The token response includes a scope field, a space-separated list of granted scopes. Check it at startup and fail early.
This script uses the client credentials flow and exits if api is missing:
import os
import sys
import requests
MY_DOMAIN = os.environ["SF_MY_DOMAIN"] # e.g. https://mydomain.my.salesforce.com
REQUIRED_SCOPES = {"api"}
resp = requests.post(
f"{MY_DOMAIN}/services/oauth2/token",
data={
"grant_type": "client_credentials",
"client_id": os.environ["SF_CONSUMER_KEY"],
"client_secret": os.environ["SF_CONSUMER_SECRET"],
},
timeout=30,
)
resp.raise_for_status()
token = resp.json()
granted = set(token.get("scope", "").split())
missing = REQUIRED_SCOPES - granted
if missing:
sys.exit(f"Token is missing required scopes: {sorted(missing)}")
print("Granted scopes:", sorted(granted))If scope is empty or absent in a response, treat the grant as unknown and test with a real API call rather than guessing.
Scope changes and existing tokens
Scopes are fixed into a token when it is issued. If you add a scope to the connected app later, tokens already in circulation do not gain it. For the web server flow, users must authorize again, and you may need to revoke their earlier approvals first. For JWT bearer and client credentials, request a new token and the new scope appears.
Removing a scope from the connected app takes effect for new tokens. Revoke old tokens if the removal is a security fix.
Choosing a minimal set
Use this order of questions when you configure a connected app.
- Does the app call REST, SOAP, or Bulk APIs? Select
api. - Does the app run as a user who is present, and must it keep working after the access token expires? Select
refresh_token. - Does the app need to know who signed in? Select
openid, and addprofileoremailonly for fields you will use. - Does the app send the user into the Salesforce UI? Select
web. - Everything else stays off until a feature needs it.
Common mistakes
- Selecting
fulland expecting a refresh token. Addrefresh_tokenas well. - Requesting a scope in the URL that the connected app does not list, which returns
invalid_scope. - Adding
refresh_tokento a JWT bearer or client credentials app and expecting a refresh token back. Neither flow issues one. - Treating scopes as object-level security. They are not. Use permission sets for that.
- Forgetting that changes to the connected app's scope list do not alter tokens that already exist.
Exercise
An integration uses the web server flow with scope full. After the access token expires, the app has no refresh token and users must log in again. What is wrong, and what is the smallest fix?
Show answer
full does not include the ability to issue refresh tokens. Add refresh_token (or offline_access) to the connected app's selected scopes and to the scope parameter of the authorization request, for example scope=full%20refresh_token. Better, replace full with api%20refresh_token if the app only calls the API. Existing users must authorize again, because their current tokens were issued without the scope.
Exercise
A client credentials integration's token response contains "scope": "api". The connected app was later edited to also include custom_permissions. The integration still cannot read custom permissions. Why, and how do you fix it?
Show answer
Scopes are fixed when a token is issued, so the existing token still carries only api. Request a new access token from the token endpoint. The new response's scope field should then include custom_permissions. Check the field before relying on the new scope.