401 似乎用于表示身份验证失败,而 403 表示授权失败(这意味着身份验证成功?)
在 oauth 流的情况下,如果我尝试使用过期的 token 进行身份验证,提示客户端刷新 token 并重试的正确错误代码是什么?
最佳答案
A 401。401 与 403 的混淆并不新鲜。请参阅 w3.org 电子邮件中的一些讨论:https://lists.w3.org/Archives/Public/ietf-http-wg/2008AprJun/0418.html ,尽管引用的 RFC 2616 已过时。
关于 401,见 https://tools.ietf.org/html/rfc7235#section-3.1
关于 403,见 https://tools.ietf.org/html/rfc7231#section-6.5.3
从 401 描述中,虽然“有效”有待解释:
The 401 (Unauthorized) status code indicates that the request has not been applied because it lacks valid authentication credentials for the target resource.
但更相关的是,请参阅关于承载 token 身份验证的 RFC 6750,第 3 节,最后一段。
https://tools.ietf.org/html/rfc6750#section-3
And in response to a protected resource request with an authentication attempt using an expired access token:
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer realm="example",
error="invalid_token",
error_description="The access token expired"
关于http-authentication - 身份验证 token 到期的正确 HTTP 代码 - 401 或 403?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45153773/