Sub topic 4
Choosing OAuth scopes
This chapter assumes you can create a connected app or external client app (ECA) and find its OAuth settings in Setup. You should also know the consumer key and callback URL from the previous chapter.
What a scope does
A scope is a named permission that an OAuth app asks for. The user or admin approves it, and Salesforce records it on the access token. Scopes limit what the token can do. They never add permissions. A token with the api scope still sees only what the running user's profile and permission sets allow.
Scopes work at two levels:
- The app lists the scopes it is allowed to request. This is part of the OAuth settings.
- Each authorization request names the scopes it wants. Salesforce rejects any scope that the app did not list.
Common scopes
| Scope | What it allows |
|---|---|
| `api` | Calls to REST, SOAP, Bulk, and similar APIs as the user |
| `refresh_token` (also `offline_access`) | Receiving a refresh token, so the app can get new access tokens without the user present |
| `web` | Using the access token in a browser, for example to open Salesforce pages |
| `full` | Everything the user can access. This scope does not include a refresh token |
| `openid` | An ID token that identifies the user (OpenID Connect) |
| `id` | The identity URL service, which returns basic user information |
| `custom_permissions` | Reading which custom permissions the user has |
Salesforce also offers product-specific scopes, such as chatter_api and wave_api. The list in Setup depends on the features in your org.
Choose the smallest set
Start from what the integration actually calls. Add a scope only when a call fails without it.
- A nightly sync that calls the REST API needs
api. If it uses the JWT bearer flow, it does not needrefresh_token, because it can sign a new JWT whenever it needs a token. - A web app where users log in and stay signed in needs
apiandrefresh_token. Addopenidonly if the app reads the ID token. - Avoid
fullunless you have a specific reason. It is much broader thanapi, and it still requiresrefresh_tokenas a separate scope.
# Request only what the app needs: API access and a refresh token
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&code_challenge=$CODE_CHALLENGE&code_challenge_method=S256"Scopes in the URL are separated by a space, written as %20. The token response includes a scope field. Compare it with what you requested to see what was granted.
Scopes versus policies
Scopes describe what the app may ask for. Policies, covered later, describe who may use the app and how long tokens last. A narrow scope does not replace a restrictive policy, and a restrictive policy does not replace a narrow scope. Use both.
Exercise
A nightly integration uses the JWT bearer flow to call the REST API. Which scopes should its app include, and which should it leave out?
Show answer
Include api. Leave out refresh_token, because the integration can sign a new JWT whenever it needs a token. Leave out full, because it is broader than the integration needs.