Lab-tested: Python 3.13.5 mock API + Apache 2.4.71 · 2 Dec 2026 · Reproduced and fixed in our lab
Fix Box — Try This First (10 Seconds)
401 means the server does not accept your credentials — missing, wrong, or expired. 403 means the server knows (or does not care) who you are and still refuses.
The test: retry with fresh, valid credentials. If the answer changes, it was a 401 problem.
If it does not, stop touching credentials and look at permissions, scope, or IP rules. Start from the pillar 401 Unauthorized: Causes and How to Fix It or run the 401 Checker.
Table of Contents
- The One-Line Rule
- The Exact Responses (Verbatim from Our Lab)
- The Decision Matrix (Measured)
- When 401 Is Correct
- When 403 Is Correct
- The Grey Zones
- Our Lab Reproduction
- The Fix, Step by Step
- What Did Not Work
- Prevention
- FAQ
- Sources and Date
- References
The One-Line Rule
401 = not authenticated. 403 = authenticated (or authentication is irrelevant) but not allowed.
The names mislead: "Unauthorized" actually means unauthenticated, and "Forbidden" is the true authorization refusal. RFC 9110 draws the line exactly there — 401 for requests lacking valid credentials, 403 for requests the server understood but refuses to fulfill.
The practical consequence: a 401 can often be fixed by the client alone (send credentials, refresh the token). A 403 almost never can. Retrying a 403 with fresher credentials is the most common wasted hour in API debugging.
The Exact Responses (Verbatim from Our Lab)
Same endpoint, same lab API, three credential states — captured 2 Dec 2026:
# No credentials
HTTP/1.0 401 Unauthorized
WWW-Authenticate: Bearer realm="api"
# Valid token, insufficient scope (needs "admin", token has "read")
HTTP/1.0 403 Forbidden
WWW-Authenticate: Bearer realm="api", error="insufficient_scope", scope="admin"
# Valid token, correct scope
HTTP/1.0 200 OK
{"ok": true, "admin": true}
Notice the 403 still carries a challenge. That is the RFC 6750 insufficient_scope case: the server tells you which scope would work, but no amount of re-authenticating with the same scope changes the answer.
The Decision Matrix (Measured)
We ran the full matrix against our mock API and an Apache IP-restricted location. Every cell below is a measured result, not a guess:
| Credentials sent | Resource rule | Result | Why |
|---|---|---|---|
| None | Token required | 401 | No identity presented |
| Bogus token | Token required | 401 invalid_token | Identity presented but not recognized |
| Expired token | Token required | 401 invalid_token | Identity expired; refresh fixes it |
Valid token, scope read | Needs scope admin | 403 insufficient_scope | Identity fine, permission missing |
Valid token, scope admin | Needs scope admin | 200 | Identity and permission both fine |
| Valid admin token | IP not allowlisted | 403 | Refusal unrelated to credentials |
| None (Apache, IP-restricted location) | Require ip | 403 | Apache refuses before auth even matters |
The last two rows are the ones that break the naive rule "403 means logged in but not allowed." Sometimes nobody is logged in and it is still 403, because the refusal has nothing to do with identity.
When 401 Is Correct
Send 401 when credentials are absent, malformed, expired, revoked, or simply wrong. The response MUST include a WWW-Authenticate challenge naming the expected scheme.
The client may retry with new or replaced credentials — that retry path is the whole point of the status.
Typical correct 401s: an API call with no token, a JWT past its exp, a Basic login with a wrong password, an API key that was rotated away. Each is fixable by the client: authenticate, refresh, or correct the value.
See 401 JWT/Bearer Token Errors and 401 Invalid API Key Errors.
When 403 Is Correct
Send 403 when the request is understood and refused for reasons credentials cannot change. Valid user, wrong role.
Valid token, missing scope. Source IP outside the allowlist. A resource that exists but is closed to this account.
RFC 9110 adds a nuance: if credentials were sent, a 403 means the server judged them insufficient, and re-authenticating will not help. If the server wants to hide that the resource exists at all, it may answer 404 instead — a deliberate choice, not an error.
The Grey Zones
Real systems blur the line in a few known places. Some APIs return 403 for expired tokens, which breaks client refresh logic that keys on 401.
Some return 401 for insufficient scope, which sends clients into refresh loops. WAFs and CDNs often answer 403 for bot rules regardless of credentials — that 403 is about the request's fingerprint, not the user.
When you consume an API, do not assume the textbook mapping. Read the body and the challenge.
When you build an API, pick the textbook mapping and document it; your integrators' retry logic depends on it. Our WWW-Authenticate Challenges Table shows how the header disambiguates most grey cases.
Our Lab Reproduction
Stack: Python 3.13.5 mock API (Bearer with scopes, IP-deny endpoint) and Apache 2.4.71 (IP-restricted location), tested 2 Dec 2026. Seven matrix cells measured: three 401 states, two credential-related 403 states, one IP-based 403 on each stack, and the 200 control case. All matched the RFC 9110 definitions.
Honest lab note: the lab implements the textbook mapping on purpose. Production APIs deviate, especially behind WAFs. The matrix is a diagnostic baseline: when a real response disagrees with it, the disagreement itself is information about that system's conventions.
The Fix, Step by Step
- Capture the response with headers:
curl -si https://your-api.example/endpoint. - If 401: read the challenge. No credentials → add them.
invalid_token→ refresh or re-login (Prevent 401: Token Refresh Done Right).invalid_request→ fix the header shape (Authorization Header Hygiene). - If 403: check
errorfirst.insufficient_scope→ request a token with the named scope. No error value → check role/permission assignments, IP allowlists, and WAF rules. Do not rotate credentials; they are not the problem. - If the status contradicts the matrix (403 for an expired token, 401 for a scope problem), document that API's convention in your client and branch on the error body instead of the status alone.
- Verify with the control case: known-good credentials with the right scope from an allowed network must return 200. If it does not, the problem is server-side config, not your client.
What Did Not Work
Refreshing the token after a scope-based 403 did not work in our lab: the new token carried the same scope and earned the same 403. Freshness is not permission.
Re-sending Basic credentials to an IP-restricted Apache location also changed nothing — Apache returned 403 before authentication was relevant. When the refusal is network-level, only the network rule can change it.
Treating every 403 as "log in again" in client code creates login loops: the user authenticates successfully, retries, gets the same 403, and is bounced to login forever. Branch on the status: 401 → authenticate; 403 → show an access-denied state.
Prevention
For API builders: return 401 only for credential problems, always with a challenge; return 403 for permission and policy refusals, with an error value naming the reason. For client builders: key refresh logic on 401 (plus invalid_token), and treat 403 as terminal for that request.
Both sides should log status, error value, and scope — the trio that separates these cases in one glance.
FAQ
What is the difference between 401 and 403 in one sentence? 401 means "we do not accept your credentials — authenticate (again)"; 403 means "we refuse this request — credentials will not change it."
Can a 403 response include WWW-Authenticate? Yes. The insufficient_scope case pairs 403 with a Bearer challenge naming the required scope. It is the main exception to "403 has no challenge."
My token expired but the API returned 403. Is that wrong? It deviates from RFC 9110/6750 conventions, but some APIs do it. Handle it by branching on the error body for that provider, and document the deviation in your client.
Should a missing resource return 403 or 404? If revealing existence leaks information, RFC 9110 explicitly allows 404 instead of 403. For public resources where existence is not sensitive, 403 is clearer.
Why does the browser not prompt for login on 403? Browsers prompt on 401 with a Basic/Digest challenge. A 403 signals that authentication will not help, so no prompt is shown — which is correct behavior for permission refusals.
Sources and Date
Lab tests run 2 Dec 2026 on Python 3.13.5 (mock API) and Apache 2.4.71 (Debian), localhost. Matrix cells are measured results from that run.
References verified live on 2 Dec 2026. This page is the canonical 401-vs-403 comparison for the diagnostic-center family.
References
- RFC 9110, HTTP Semantics, Sections 15.5.2 (401) and 15.5.4 (403): https://www.rfc-editor.org/rfc/rfc9110.html
- RFC 6750, OAuth 2.0 Bearer Token Usage, Section 3.1 (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
- MDN Web Docs, HTTP authentication: https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Authentication





Leave a Reply