Sub topic 17
Troubleshooting common OAuth errors
This chapter assumes you have run at least one of the three flows from earlier chapters. You should be comfortable with curl, token endpoints, and connected app settings.
Read the error before changing anything
A failed token request returns HTTP 400 with a JSON body that has two fields. error is a short code. error_description gives the specific reason. The description is usually what tells you what to fix, so read it first.
curl -s -X POST https://login.salesforce.com/services/oauth2/token \
-d grant_type=refresh_token \
-d refresh_token=<REFRESH_TOKEN> \
-d client_id=<CONSUMER_KEY> \
-d client_secret=<CONSUMER_SECRET>A failure looks like this:
{"error":"invalid_grant","error_description":"expired access/refresh token"}The common errors
| Error | Typical cause | Fix |
|---|---|---|
| `redirect_uri_mismatch` | The `redirect_uri` you sent differs from the connected app's callback URL. | Make them match exactly, including scheme, host, path, and trailing slash. |
| `invalid_client_id` | The consumer key is wrong, or you sent it to the wrong login host. | Recopy the key. Use `test.salesforce.com` for sandboxes. |
| `invalid_client` | The consumer secret is wrong, or a secret is required and missing. | Check the secret and the app's secret requirements. |
| `invalid_grant` | The code, refresh token, or assertion was rejected. See below. | Match the flow to the cause. |
| `unsupported_grant_type` | The flow is not enabled on the app, or the host is wrong. | Enable the flow. For client credentials, call the My Domain URL. |
Why `invalid_grant` has many causes
This one error covers several different problems, so the description matters most here.
- Web server flow: an authorization code is single-use and short-lived. Reusing it or waiting too long fails. A wrong
code_verifierfails too, because it must match the earlier challenge. - Refresh: the refresh token was revoked, expired by policy, or issued to a different app.
- JWT bearer: the user is not pre-authorized, the
auddoes not match the login host,exphas passed, or the uploaded certificate does not match the signing key. - Client credentials: the app has no run-as user set, which gives a "no client credentials user enabled" description.
Errors on API calls
A call to the REST API can fail after authentication succeeds. INVALID_SESSION_ID with HTTP 401 means the access token has expired or been revoked. Refresh it, or request a new one. Also confirm you are calling the instance_url from the token response.
Exercise
A JWT bearer request returns invalid_grant with the description "user hasn't approved this consumer". The signature and expiry are fine. What do you check next?
Show answer
Check pre-authorization. The connected app must use "Admin approved users are pre-authorized", and the integration user's profile or a permission set must be assigned to the app.
Exercise
A client credentials call to login.salesforce.com fails. Which host should you use, and which app setting must also be in place?
Show answer
Use the org's My Domain URL (MyDomainName.my.salesforce.com). The connected app must also have the client credentials flow enabled and a run-as user assigned.