Sub topic 19
Troubleshooting common OAuth errors
This chapter assumes you can request tokens with the authorization code with PKCE, JWT bearer, and client credentials flows, and that you know where a connected app or external client app (ECA) keeps its OAuth settings and policies. Here you learn to read the error Salesforce returns, find the setting that caused it, and confirm the fix.
Read the error before changing anything
A failed token request returns an HTTP 400 or 401 with a JSON body that has two fields:
erroris a short code such asinvalid_grant.error_descriptiongives the specific reason.
The error code is broad. invalid_grant alone covers at least six different causes, so the description is what points you to the fix. Capture the whole body. Do not rely on the HTTP status.
The following script sends a client credentials request and prints the status and body, so you see the error text instead of an empty failure:
#!/usr/bin/env bash
# Usage: CONSUMER_KEY=... CONSUMER_SECRET=... ./token.sh https://MyDomain.my.salesforce.com
set -u
HOST="$1"
curl -sS -o body.json -w 'HTTP %{http_code}\n' \
-X POST "$HOST/services/oauth2/token" \
-d grant_type=client_credentials \
-d client_id="$CONSUMER_KEY" \
-d client_secret="$CONSUMER_SECRET"
cat body.json
echoA typical failure looks like this:
{
"error": "invalid_grant",
"error_description": "user hasn't approved this consumer"
}A decision path
Start from the error code, then narrow by the description.
Common errors and fixes
| Error and description | Usual cause | Fix |
|---|---|---|
| `invalid_client_id`: client identifier invalid | Wrong consumer key, the app is in a different org, or the app was created minutes ago | Copy the key again. Confirm the org. Wait a few minutes after creating a new app. |
| `invalid_client`: invalid client credentials | Wrong or rotated consumer secret | Use the current secret. Rotate it if you are unsure. |
| `redirect_uri_mismatch` | The `redirect_uri` sent differs from the callback URL in the app | Make them match character for character, including scheme, host, path, and trailing slash. |
| `unsupported_grant_type` | The flow is not enabled for the app | Enable the flow in the OAuth settings (for ECAs, in the app's OAuth settings). |
| `invalid_grant`: user hasn't approved this consumer | Permitted Users is "Admin approved users are pre-authorized" and the user has no matching profile or permission set | Assign the app through a profile or permission set. |
| `invalid_grant`: ip restricted | IP relaxation enforces login IP ranges and the caller is outside them | Adjust IP relaxation or the allowed ranges. Prefer a fixed egress IP. |
| `invalid_grant`: authentication failure | The signed JWT does not match the uploaded certificate, or the user is not pre-authorized | Upload the certificate that matches your private key. Check the approval setup. |
| `invalid_grant`: invalid assertion | A bad `iss`, `sub`, `aud`, or `exp` claim | Check each claim, as described below. |
| `invalid_grant`: expired authorization code | The code was used twice or waited too long | Restart the flow. Codes are single use and short lived. |
| `invalid_grant` on code exchange with PKCE | The `code_verifier` does not hash to the original `code_challenge` | Reuse the same verifier from the same session. Check the S256 encoding. |
| "request not supported on this domain" | Client credentials sent to a generic login URL | Send the request to your My Domain URL. |
Flow-specific checks
Authorization code with PKCE
Most failures come from state that differs between the two steps. Check that:
- The
redirect_uriis identical in the authorize request and the token request. - The verifier is stored per login attempt, not shared across users or browser tabs.
- The challenge uses base64url encoding with no
=padding and with+/replaced by-_. - Your
scopevalues are enabled in the app. A scope you request but did not allow is rejected or dropped.
JWT bearer
Check each claim in turn:
issmust be the consumer key of this app, not the secret.submust be the Salesforce username, not the email address if the two differ.audmust be the login URL that fits the org:https://login.salesforce.comfor production,https://test.salesforce.comfor a sandbox, or the My Domain URL.expmust be a Unix timestamp in the future and no more than about three minutes ahead. A server clock that runs fast or slow causes failures that look random.- The user must be pre-authorized, and the certificate in the app must match the signing key.
Client credentials
Run-as configuration causes most failures. Confirm that the flow is enabled, that a run-as user is set in the app's policies, and that you call the My Domain URL. If the run-as user is deactivated or loses access to the app, tokens stop issuing.
When the token works but the API call fails
Test the token against the API directly. This separates authentication faults from permission faults:
curl -i https://MyDomain.my.salesforce.com/services/data/ \
-H "Authorization: Bearer $ACCESS_TOKEN"A 401 usually means the session expired, was revoked, or you sent the token to the wrong instance. Use the instance_url returned with the token. A 403 usually means the user lacks object, field, or API permission, or the token's scopes do not cover the call.
Use logs to confirm the cause
When the description is vague, check the org's own records. Login History shows failed OAuth attempts with the status and source IP. This helps confirm an IP restriction or a user mismatch. The OAuth usage page lists which users hold tokens for the app, and you can revoke them there. Revoke a token, request a new one, and retest after every policy change. Policy changes do not always affect tokens already issued, so a stale token can hide a fix or a fault.
Exercise
A CI job using the JWT bearer flow starts failing with invalid_grant and the description "user hasn't approved this consumer". The certificate and key are unchanged. Name two settings to check and one to rule out.
Show answer
Check Permitted Users: if it is "Admin approved users are pre-authorized", confirm the integration user's profile or a permission set assigned to them includes the app. Also check that the integration user is active and the username in sub is correct. Rule out the certificate: a certificate mismatch gives "authentication failure", not "hasn't approved this consumer".
Exercise
A client credentials request to https://login.salesforce.com/services/oauth2/token fails, but the same request works against your My Domain URL. Why?
Show answer
The client credentials flow needs the My Domain URL to identify the org. The generic login URL cannot resolve which org's app and run-as user to use.