Sub topic 17
Troubleshooting common OAuth errors
This chapter assumes you have configured a connected app and made token requests with at least one of the three flows: web server, JWT bearer, or client credentials. It also assumes you can read an HTTP response and run curl. The goal is to turn an error response into a specific fix without guessing.
How Salesforce reports OAuth errors
Failures at the token endpoint return a JSON body with two fields: error is a short code, and error_description is a human-readable hint. The HTTP status is usually 400, or 401 for some client failures.
{
"error": "invalid_grant",
"error_description": "user hasn't approved this consumer"
}The error value tells you the category. The error_description tells you which setting to check. Read both before changing anything. Salesforce also writes some errors to the browser instead of returning JSON. This happens when the failure occurs on the authorization endpoint, before any token request exists. Those errors appear as an error page or as query parameters on your redirect URI.
The errors below are the ones you will meet most often. After the sections for each error, a decision table maps symptoms to flows.
redirect_uri_mismatch
This error comes from the web server flow. The redirect_uri in your request must match a callback URL registered on the connected app. The match is exact, so check these points:
- The scheme must match.
httpandhttpsare different. - The host, port, and path must match, including a trailing slash.
- The value in the authorization request and the value in the token request must be identical.
- A URL-encoded value in the browser URL must decode to the registered string.
To fix it, copy the callback URL from the connected app and compare it character by character with what your code sends. Many teams register several callback URLs, one per environment, and then send the wrong one from a test machine.
invalid_client_id and invalid_client
The two names are close but point at different problems.
invalid_client_id means Salesforce does not recognize the consumer key you sent. Common causes:
- You used the consumer secret, or an old key, in the
client_idfield. - You sent a production key to
test.salesforce.com, or a sandbox key tologin.salesforce.com. - The connected app was created moments ago. Newly created apps can take a few minutes to become usable.
- The key has a stray space or newline from copy and paste.
invalid_client means the key was found but authentication failed. The usual cause is a wrong or missing client_secret. It can also appear when a flow needs the secret and the app is configured not to require one, or the other way around. Check the connected app's settings for the secret requirement and for the "Require Secret for Web Server Flow" style options.
invalid_grant
invalid_grant is the most common and the least specific. The error_description is what narrows it. This table lists the descriptions you are likely to see.
| error_description | Flow | Likely cause | Fix |
|---|---|---|---|
| `expired authorization code` | Web server | The code was used late. Codes are short-lived. | Exchange the code immediately after the redirect. |
| `authentication failure` | Web server | The code was already used, or the verifier does not match. | Request a new code. Send the same `code_verifier` that produced the challenge. |
| `invalid assertion` | JWT bearer | The signature, `aud`, `iss`, or `exp` is wrong. | Check the claims and the key pair (see below). |
| `user hasn't approved this consumer` | JWT bearer | The user is not pre-authorized. | Set the policy to admin-approved users and assign a profile or permission set. |
| `no client credentials user enabled` | Client credentials | No run-as user is set. | Select a run-as user on the connected app. |
| `expired access/refresh token` | Refresh | The refresh token was revoked or its policy expired. | Send the user through the web server flow again. |
Diagnosing a JWT bearer failure
Most invalid assertion errors come from a short list of mistakes. Check them in this order:
issmust be the consumer key of the connected app, not the username.submust be the username of the user, and that user must be pre-authorized.audmust be the login URL:https://login.salesforce.com,https://test.salesforce.com, or your My Domain URL. A mismatch with the endpoint you post to fails the request.expmust be in the future, and no more than a few minutes ahead. Clock drift on the server can make a fresh token look expired, so sync the clock with NTP.- The certificate uploaded to the connected app must match the private key that signs the assertion. If you regenerated the key pair, upload the new certificate.
- The algorithm must be RS256.
The script below decodes your assertion without verifying it, so you can inspect the claims. It uses PyJWT.
import jwt # PyJWT
def inspect_assertion(assertion: str) -> None:
header = jwt.get_unverified_header(assertion)
claims = jwt.decode(assertion, options={"verify_signature": False})
print("alg:", header.get("alg"))
for name in ("iss", "sub", "aud", "exp"):
print(f"{name}:", claims.get(name))
if __name__ == "__main__":
import sys
inspect_assertion(sys.argv[1])Run it with the assertion as an argument and compare each printed value with the connected app and the endpoint you call.
Diagnosing a client credentials failure
The request carries no user, so the failures are about configuration. Confirm that the flow is enabled on the connected app, that a run-as user is selected, and that you post to the My Domain URL rather than login.salesforce.com. If the run-as user is deactivated or lacks the API Enabled permission, the token request or the following API call fails.
unsupported_grant_type and invalid_request
unsupported_grant_type means the grant_type value is not recognized or the flow is not enabled for this app. Check the spelling. The JWT bearer value is the long string urn:ietf:params:oauth:grant-type:jwt-bearer. Also confirm the matching option is enabled on the connected app.
invalid_request usually means a required parameter is missing or the body is not form-encoded. With curl, use -d for each field so the request is sent as application/x-www-form-urlencoded. If you send JSON, the endpoint rejects it.
API errors after a successful token
A valid token does not guarantee a successful API call. These errors come from the REST API, not the OAuth endpoints:
401withINVALID_SESSION_IDmeans the access token expired or was revoked. Refresh it, or repeat the flow.403withREQUEST_LIMIT_EXCEEDEDmeans the org hit an API limit.403orINSUFFICIENT_ACCESSmeans the user or run-as user lacks object or field permission. Scopes do not grant data access. Profiles and permission sets control that.- A
404or connection error often means you usedlogin.salesforce.cominstead of theinstance_urlfrom the token response.
curl -i https://MyDomainName.my.salesforce.com/services/data/v60.0/limits \
-H "Authorization: Bearer <ACCESS_TOKEN>"The -i flag prints the status line and headers, so you can separate an HTTP failure from a body-level error.
A repeatable debugging routine
- Reproduce the failure with
curlso you remove your application code from the picture. - Read
erroranderror_descriptiontogether. - Confirm the host: login URL, sandbox URL, or My Domain.
- Compare the consumer key, secret, and callback URL with the connected app, character by character.
- For user-based flows, check the user's profile or permission set and the app's policies.
- In Setup, open Login History for the user. It records failed attempts with a reason that often matches the error.
Exercise
Your JWT bearer request returns invalid_grant with the description "user hasn't approved this consumer". The signature and claims are correct. What do you change, and where?
Show answer
The user is not pre-authorized for the connected app. In the connected app's policies, set Permitted Users to "Admin approved users are pre-authorized", then assign the user's profile or a permission set to the connected app. Retry the same assertion after saving.
Exercise
A web server flow login fails on the authorization page with redirecturimismatch. Your code sends https://app.example.com/oauth/callback, and the connected app lists https://app.example.com/oauth/callback/. What is wrong, and how do you fix it?
Show answer
The registered URL has a trailing slash and the request does not, and Salesforce requires an exact match. Either add the exact URL without the slash to the connected app's callback URLs or change the code to send the registered value. Use the same value in the token request.