403 Forbidden in APIs: When Your Token Is Valid but Not Allowed

403 Forbidden in APIs: When Your Token Is Valid but Not Allowed

Written by

in

Lab-tested: Local lab server (Python 3.13.5, nginx-style log format) · 2 Dec 2026 · Reproduced and fixed in our lab

Fix Box — Try this first (10 seconds)

Read the response body before you touch the token. A 403 with “insufficient scope” means your token is valid but underpowered — request a token with the right scope.

A 401 means the token itself is missing or expired. Our 403 vs 401 vs 404 guide maps the split.

The API split: 401 for identity, 403 for permission

APIs use the two codes with strict discipline. 401 answers identity problems: no token, malformed token, expired token.

The response usually carries WWW-Authenticate: Bearer with an error value such as invalid_token.

403 answers permission problems: the token is valid, but its scope, role, or account plan does not allow this action.

RFC 6750 names this case insufficient_scope. Retrying with the same token will never fix it.

Our lab test

We measured all four cases against a local mock API on 2 Dec 2026:

no token            -> HTTP 401  {"error": "unauthorized", "detail": "missing token"}
expired token       -> HTTP 401  {"error": "unauthorized", "detail": "token expired"}
read-only scope     -> HTTP 403  {"error": "forbidden", "detail": "scope write required", "required_scope": "write"}
full scope token    -> HTTP 200  {"ok": true, "data": [1, 2, 3]}

The pattern is exact. Identity failures returned 401 with a WWW-Authenticate header. The one permission failure returned 403 with a JSON body naming the required scope.

The body, not the status code alone, told us the fix.

Reading the error body

Good APIs put the reason in the body. Look for fields like error, detail, required_scope, or a documentation URL.

Our lab body named the missing scope directly: "required_scope": "write".

If the body is empty, check the headers. A WWW-Authenticate header with insufficient_scope is the RFC 6750 form of the same message.

Some APIs also return a Retry-After or rate-limit headers — those belong to 429, not 403.

Step-by-step fix

First, capture the full response: status, headers, and body. Do not log the token itself. Second, classify: 401 means fix identity, 403 means fix permission.

For 401, check the Authorization: Bearer <token> header shape — scheme, one space, token. Then check expiry and refresh the token.

Our sister site 401.my.id covers token failures in depth.

For 403, read the required scope from the body and request a new token with that scope. If your account plan gates the endpoint, no token will help — the fix is a plan or role change on the provider side.

Confirm the fix by calling a harmless read endpoint first, then the real one.

What Did Not Work

Refreshing the token did nothing for the 403 case in our test. The refreshed token carried the same read-only scope. Freshness was never the problem.

Retrying in a loop also failed, predictably. A permission decision is deterministic. The same token against the same endpoint returns the same 403 until the scope changes.

Prevention for API teams

Request the scopes you need at token creation time, and document them per endpoint. In client code, treat 401 and 403 as different branches: refresh on 401, surface a permission error on 403.

Never retry 403 automatically.

On the server side, always name the required scope in the 403 body. It turns a support ticket into a self-service fix.

The 403 pillar guide covers the non-API side of the same code.

FAQ

Frequently Asked Questions

My token works on one endpoint but 403s on another. Why?

Scopes are per-endpoint. Your token covers the first endpoint’s scope but not the second. Request a token with both scopes.

Is 403 ever correct for a missing token?

No. A missing or invalid token is a 401. APIs that answer 403 there are hiding information deliberately, which breaks client refresh logic.

The body is empty. How do I debug?

Check the `WWW-Authenticate` header for `insufficient_scope`, then check your token’s scopes in the provider dashboard. If both are clean, ask the provider — an empty 403 is their bug to explain.

Does a 403 count against my rate limit?

Usually the request still counts, but that is provider-specific. The rate-limit answer itself is 429, not 403.

Sources and date

Lab test: 2 Dec 2026, local mock API on our SI machine (Python 3.13.5; client IP is localhost).

The four responses above are verbatim measured output, including the JSON bodies.

Measured requests:

curl https://api.example.com/data                                  # -> 401 missing token
curl -H "Authorization: Bearer <expired>"  https://api.example.com/data  # -> 401 token expired
curl -H "Authorization: Bearer <read-only>" https://api.example.com/data # -> 403 scope write required
curl -H "Authorization: Bearer <full>"      https://api.example.com/data # -> 200

References

  • RFC 9110 §15.5.3 — 403 Forbidden: https://www.rfc-editor.org/rfc/rfc9110.html
  • RFC 6750 — OAuth 2.0 Bearer Token Usage (insufficient_scope): https://www.rfc-editor.org/rfc/rfc6750.html
  • MDN Web Docs — 401 Unauthorized: https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/401
  • MDN Web Docs — 403 Forbidden: https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/403

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *


More posts