Sub topic 3
OAuth settings: consumer key, secret, and callback URL
This chapter assumes you can open Setup, create a connected app, and read an HTTP request. You should also know the difference between authentication and authorization. Here you learn the three values that identify and protect an OAuth client in Salesforce.
Where the settings live
OAuth is off by default for a connected app. In Setup, open App Manager, create or edit a connected app, and select Enable OAuth Settings. Three things then matter most: the consumer key, the consumer secret, and the callback URL.
Consumer key
The consumer key is Salesforce's name for the OAuth client_id. It identifies your app in every authorization and token request.
- Salesforce generates it when you save the app.
- It is an identifier, not a password. It appears in browser URLs, so treat it as public.
- A newly saved app can take a few minutes to become usable. Requests made too early fail with
invalid_client_id.
Consumer secret
The consumer secret is the client_secret. It proves that the request comes from your app and not from someone who copied the key.
- Reveal it in the app's detail page. Salesforce may ask you to verify your identity first.
- Store it in a secret manager or an environment variable. Never commit it to source control or ship it in a mobile app or browser code.
- Clients that cannot keep a secret should use PKCE instead. Those are single-page apps, mobile apps, and CLI tools.
- If the secret leaks, generate a new one and update every client that uses it.
The app also has options for when the secret is required. Require Secret for Web Server Flow and Require Secret for Refresh Token Flow are on by default. Require Proof Key for Code Exchange (PKCE) makes the authorization code flow demand a code challenge.
Callback URL
The callback URL is the redirect_uri. After the user signs in, Salesforce sends the browser back to this address with an authorization code.
- Salesforce compares the
redirect_uriin the request to the registered values exactly. A different path, a trailing slash, or a different scheme causes a mismatch. - You can register several URLs, one per line. Use this for development, test, and production.
- Use HTTPS.
http://localhostis allowed for local development. Mobile apps can use a custom scheme such asmyapp://callback.
This command prints an authorize URL that uses all three values. Only the key and callback URL appear in the URL. The secret is sent later, in the token request.
CONSUMER_KEY="3MVG9...example"
CALLBACK="https%3A%2F%2Fapp.example.com%2Fcallback"
echo "https://MyDomain.my.salesforce.com/services/oauth2/authorize?response_type=code&client_id=$CONSUMER_KEY&redirect_uri=$CALLBACK"Common errors
| Error | Likely cause |
|---|---|
| `redirect_uri_mismatch` | The request's `redirect_uri` is not registered in the app. |
| `invalid_client_id` | The consumer key is wrong, or the app was saved only moments ago. |
| `invalid_client` | The secret is wrong, or a secret is required and was not sent. |
External client apps
External client apps keep the same three values. The consumer key and secret sit in the developer-owned OAuth settings, which a later chapter covers.
Exercise
A web app gets redirect_uri_mismatch right after you add a staging environment at https://staging.example.com/callback. The app definition lists only the production callback. What do you change, and does the consumer key change?
Show answer
Add https://staging.example.com/callback to the app's callback URLs, one URL per line, and save. The consumer key stays the same, because the key identifies the app and not the environment.