Sub topic 18
Troubleshooting common OAuth errors
This chapter assumes you can configure a connected app or external client app (ECA), and that you know the authorization code with PKCE, JWT bearer, and client credentials flows. It also assumes you can use curl and read JSON responses. The aim is to map an error message to its usual cause and fix.
Read the error first
A failed token request returns JSON with an error code and an error_description. The error code names the category. The description usually holds the detail you need.
{
"error": "invalid_grant",
"error_description": "user hasn't approved this consumer"
}Most failures come from a mismatch between what the app is configured to allow and what the request sends.
Common errors and fixes
| Error and description | Flow | Usual cause | Fix |
|---|---|---|---|
| `invalid_client_id` | All | The consumer key is wrong, or the app does not exist in this org. | Copy the consumer key again. Check you are calling the right org and My Domain. |
| `invalid_client` or `invalid client credentials` | Client credentials, auth code | The consumer secret is wrong, or the secret is required but missing. | Re-copy the secret. Check the app's secret requirements. |
| `redirect_uri_mismatch` | Auth code | The `redirect_uri` does not exactly match a registered callback URL. | Make the registered URL and the request value identical, including scheme, path, and trailing slash. |
| `invalid_grant` with `user hasn't approved this consumer` | JWT bearer | The user has not authorized the app, and the policy does not pre-authorize them. | Set permitted users to admin-approved and assign a profile or permission set. |
| `invalid_grant` with `invalid assertion` | JWT bearer | The signature does not match the uploaded certificate, or `exp` is in the past. | Sign with the matching private key. Set `exp` a few minutes ahead and check the server clock. |
| `invalid_grant` with `audience is invalid` | JWT bearer | The `aud` claim does not match the login URL. | Use `https://login.salesforce.com`, or the My Domain or sandbox URL. |
| `invalid_grant` with `expired authorization code` | Auth code | The code was redeemed late or reused. | Redeem the code immediately. Codes are single use. |
| `invalid_grant` with `invalid code verifier` | Auth code | The verifier does not hash to the challenge sent earlier. | Reuse the same verifier from the same session. Use S256 consistently. |
| `invalid_scope` | All | The request asks for a scope the app does not include. | Add the scope in the app's OAuth settings, or remove it from the request. |
| `INVALID_SESSION_ID` or `401` on an API call | API call | The access token expired or was revoked. | Request a new token with the refresh token or by repeating the flow. |
Client credentials checks
If the client credentials flow returns an error, confirm the app has a run-as user configured. Then confirm that user holds the permissions your API call needs. Without a run-as user the flow cannot issue a token.
A debugging routine
- Copy the exact
erroranderror_description. - Confirm the host: a My Domain URL, the login URL, or a sandbox URL.
- Compare the consumer key, callback URL, and scopes to the app settings.
- Check policies: permitted users, IP relaxation, and the user's profile or permission set.
- Check Login History and the connected apps OAuth usage page for the failed attempt.
- Test a good token with the REST versions resource.
curl https://MyDomain.my.salesforce.com/services/data/ \
-H "Authorization: Bearer $ACCESS_TOKEN"A JSON list of API versions means the token works. A 401 means the token is bad, and a 403 means the user lacks access.
Exercise
A CI job uses the JWT bearer flow and gets invalid_grant with user hasn't approved this consumer. The key, certificate, and aud are correct. What do you change?
Show answer
The user has not authorized the app. Set the app's permitted users policy to admin-approved users, then assign the integration user's profile or a permission set to the app. The JWT can then be exchanged without interactive approval.
Exercise
The authorization code flow returns redirect_uri_mismatch. The registered callback is https://app.example.com/callback, and the request sends https://app.example.com/callback/. What is wrong?
Show answer
The values differ by a trailing slash. The redirect URI must match a registered callback URL exactly, so make the request and the registered value identical.