Sub topic 3
OAuth settings: consumer key, secret, and callback URL
This chapter covers the three values that identify and protect an OAuth app in Salesforce: the consumer key, the consumer secret, and the callback URL. It assumes you can navigate Setup, you know what an HTTP request looks like, and you understand the difference between authentication and authorization. Scopes and flows are covered in later chapters, so this one only touches them where needed.
What the OAuth settings do
When you enable OAuth on a connected app, Salesforce generates credentials and asks you to register where users may be sent after they log in. These settings answer three questions Salesforce must settle before it issues any token:
- Who is asking? The consumer key identifies the app.
- Can the app prove it is who it says it is? The consumer secret, a certificate, or a PKCE verifier provides that proof.
- Where may the result be delivered? The callback URL limits where authorization codes and tokens can be sent.
Consumer key
The consumer key is the public identifier of the app. In OAuth terminology it is the client_id. It is a long string that starts with 3MVG. You send it in every authorization request, and in the JWT bearer flow it is the iss claim.
Treat the key as an identifier, not a secret. It appears in browser URLs, so anyone who watches a login can see it. Knowing the key does not let someone get tokens for your users. The protection comes from the other settings.
You can view the key in Setup:
- Connected app: App Manager, then the app's dropdown, then View, then Manage Consumer Details.
- External client app: Setup, then External Client App Manager, then the app's Settings tab, then OAuth Settings, then Consumer Key and Secret.
Salesforce asks you to verify your identity with a code before it shows the values. This is intentional.
Consumer secret
The consumer secret is the client_secret. It acts like a password for the app. Only a server-side component that you control should hold it.
The secret is required in some flows and absent from others:
| Flow | Uses the consumer secret? | Notes |
|---|---|---|
| Authorization code with PKCE | Optional, depending on app settings | A public client such as a mobile app or single-page app cannot keep a secret, so PKCE replaces it. |
| Client credentials | Required | The secret is the only proof of identity. |
| JWT bearer | No | A signature from the private key replaces it. |
Handle the secret the way you handle any credential:
- Keep it in a secret manager or an environment variable, never in source control, client-side JavaScript, or a mobile binary.
- Do not paste it into chat tools or tickets.
- Rotate it if it may have leaked. Setup lets you regenerate the secret, and doing so immediately breaks every integration that still uses the old value, so plan the change and update the stored value at the same time.
Requiring the secret or PKCE
The OAuth settings contain options that decide whether the app must prove itself on the token request. For the authorization code flow, two of them matter:
- Require Secret for Web Server Flow: the token request must include the consumer secret.
- Require Proof Key for Code Exchange (PKCE) extension for Supported Authorization Flows: the token request must include a
code_verifierthat matches thecode_challengesent at the start.
Enable PKCE even for apps that keep a secret. It protects against an attacker who intercepts an authorization code, because the code is useless without the verifier that only the real app holds. The two options can be combined.
Callback URL
The callback URL, also called the redirect URI, is the address where Salesforce sends the user after login, carrying the authorization code. You register one or more callback URLs in the app. Each authorization request includes a redirect_uri parameter, and Salesforce compares it with the registered list. If there is no match, the request fails with a redirect_uri_mismatch error.
This check is the main defense against code theft. If Salesforce accepted any redirect_uri, an attacker could craft a login link that sends your user's authorization code to a server the attacker controls.
Rules for callback URLs
- Use HTTPS for web apps. Salesforce allows plain
http://localhostfor local development. - The match is exact. A different path, port, trailing slash, or scheme counts as a different URL.
- Do not register wildcards or broad URLs. Register each real callback address.
- Mobile and desktop apps often use a custom scheme such as
myapp://oauth/callback. Register the same value that the app sends. - Use separate apps, or separate callback entries, for development, test, and production. Remove stale development URLs from production apps.
- Even flows that never redirect, such as client credentials and JWT bearer, require a callback URL to be filled in when you save a connected app. Any valid URL works, and it is never used. A placeholder such as
https://localhost/unusedis common. External client apps follow the same pattern for apps that enable OAuth.
URL encoding
In the authorize request, the redirect_uri value must be percent-encoded, and the value you send to the token endpoint must be identical to the one sent to the authorize endpoint. Authorization code requests fail when the two differ.
# Raw callback URL as registered in the app
CALLBACK="https://app.example.com/callback"
# Percent-encode it for the authorize URL
ENCODED=$(python3 -c "import urllib.parse,sys; print(urllib.parse.quote(sys.argv[1], safe=''))" "$CALLBACK")
echo "$ENCODED"
# https%3A%2F%2Fapp.example.com%2FcallbackThe -d redirect_uri=https://app.example.com/callback form in the token request uses the unencoded value, and curl sends it as form data. Both forms represent the same URL, so they match.
Common errors
| Error | Likely cause | Fix |
|---|---|---|
| `redirect_uri_mismatch` | The `redirect_uri` does not exactly match a registered callback URL. | Compare character by character, including scheme, port, path, and trailing slash. |
| `invalid_client_id` | The consumer key is wrong, or the app is in another org. | Copy the key again from Manage Consumer Details and check you are using the correct login host. |
| `invalid_client` | The secret is wrong, missing, or was regenerated. | Update the stored secret, and confirm the app requires one. |
| `invalid_grant` | The code expired, was used already, or the verifier does not match. | Restart the flow, and redeem each code once, quickly. |
A new app can take a few minutes before Salesforce accepts its consumer key. If a freshly created app returns invalid_client_id, wait and retry before changing anything.
Connected apps versus external client apps
Both app types hold the same three values, but the locations differ. External client apps separate the settings the developer controls, such as callback URLs, from the policies the administrator controls. Consumer details are visible in the settings, and the secret is protected the same way. The ECA chapters cover this split in detail.
Exercise
A web app returns redirect_uri_mismatch. The registered callback URL is https://app.example.com/callback, and the app sends https://app.example.com/callback/. What is wrong, and what are two ways to fix it?
Show answer
The trailing slash makes the two values different, because Salesforce matches callback URLs exactly. Fix it either by changing the app so it sends https://app.example.com/callback, or by registering https://app.example.com/callback/ as an additional callback URL. Changing the app is cleaner, since it avoids adding more registered URLs.
Exercise
Why is the consumer key safe to expose in a browser URL, while the consumer secret is not?
Show answer
The key only identifies the app. Salesforce also requires proof of identity (the secret, a PKCE verifier, or a JWT signature) and checks the callback URL before issuing tokens. The secret is that proof for some flows, so anyone who holds it can act as the app.