第三方应用程序产生错误时的响应代码应该是什么?
What should be the response code when error is generated by third party application?
我构建了一个 Web 应用程序,它基本上要求用户使用表单提供凭据(对于随机的第 3 方服务,知道哪个并不重要)。
一旦用户用第 3 方服务应用程序令牌填写表单,带有令牌的请求将发送到应用程序后端,后端获取令牌并向第 3 方服务发送请求以检查令牌是否有效。
此时,第 3 方服务 return 对后端的响应为 200 - {randomObject: object}
或 401 - Unauthorized
。
所以这是我的问题:如果第 3 方服务 returns 401,后端应该 returns 200 - false
还是 401 - Unauthorized
?
我和我的同事一直在争论这个问题。
我的观点是,对应用程序后端的访问已获得授权且参数正确(请求中存在令牌),因此响应应为 200,但响应内容应指示令牌是否有效。
他的观点是,由于令牌无效(正如第 3 方服务告诉后端的那样),后端应该 return 401 - Unauthorized
.
我们很清楚,我知道两种情况下的结果是一样的,事实上这个功能已经可以使用了,我只是想知道关于这个具体问题是否有某种约定.
谢谢
关于这个:
My point is that the access to the application backend is authorized
and the parameters are correct (a token is present in the request), so
the response should be 200
令牌的存在并不意味着请求有效。应使用 401 Unauthorized
,因为提供的凭据无效。来自 httpstatuses.com:
The request has not been applied because it lacks valid authentication
credentials for the target resource.
如果您认为它应该是 200,因为该凭证不是针对您的服务,而是针对第三方服务,因此对您的服务的授权不会失败,这可能会有争议,但在那种情况下,您仍然可能不想 return 200
而是 400 Bad Request
因为成功完成 API 操作的参数无效。
我会说它仍然应该是 401。后端的工作应该与应用程序无关,因为它的内部工作对最终用户并不重要。您的身份验证服务告诉您它无效这一事实应该足以让您抛出此错误。
我会选择 400 或 401。
401 可能会产生误导,因为对您的 API 的访问并非未经授权。 (特别是如果您的 api 也需要身份验证)
另一方面,400 可能是最好的:
The 400 (Bad Request) status code indicates that the server cannot or
will not process the request due to something that is perceived to be
a client error
我会逆势而行,你应该 return 200。
状态码401与HTTP认证有关。 W3C 对状态码有如下说法:
The request requires user authentication. The response MUST include a
WWW-Authenticate header field (section 14.47) containing a challenge
applicable to the requested resource. The client MAY repeat the
request with a suitable Authorization header field (section 14.8). If
the request already included Authorization credentials, then the 401
response indicates that authorization has been refused for those
credentials. If the 401 response contains the same challenge as the
prior response, and the user agent has already attempted
authentication at least once, then the user SHOULD be presented the
entity that was given in the response, since that entity might include
relevant diagnostic information.
(source)
由于您的服务器本身可能不使用 HTTP 身份验证,因此您不会 returning WWW-Authenticate header 挑战,因此您不会遵循此规格正确。您呼叫的第 3 方 API 可能会正确执行此操作,但顺便说一下。您的用户请求了您的页面,而不是直接从第三方 API,他们 有权这样做。您的服务器 not 决定他们不值得有效响应 - 其他人的服务器刚刚告诉您他们的令牌无效。
鉴于此,我会 return 200。请求 已 成功。您的服务器能够return信息表明第三方API调用失败。
我构建了一个 Web 应用程序,它基本上要求用户使用表单提供凭据(对于随机的第 3 方服务,知道哪个并不重要)。
一旦用户用第 3 方服务应用程序令牌填写表单,带有令牌的请求将发送到应用程序后端,后端获取令牌并向第 3 方服务发送请求以检查令牌是否有效。
此时,第 3 方服务 return 对后端的响应为 200 - {randomObject: object}
或 401 - Unauthorized
。
所以这是我的问题:如果第 3 方服务 returns 401,后端应该 returns 200 - false
还是 401 - Unauthorized
?
我和我的同事一直在争论这个问题。
我的观点是,对应用程序后端的访问已获得授权且参数正确(请求中存在令牌),因此响应应为 200,但响应内容应指示令牌是否有效。
他的观点是,由于令牌无效(正如第 3 方服务告诉后端的那样),后端应该 return 401 - Unauthorized
.
我们很清楚,我知道两种情况下的结果是一样的,事实上这个功能已经可以使用了,我只是想知道关于这个具体问题是否有某种约定.
谢谢
关于这个:
My point is that the access to the application backend is authorized and the parameters are correct (a token is present in the request), so the response should be 200
令牌的存在并不意味着请求有效。应使用 401 Unauthorized
,因为提供的凭据无效。来自 httpstatuses.com:
The request has not been applied because it lacks valid authentication credentials for the target resource.
如果您认为它应该是 200,因为该凭证不是针对您的服务,而是针对第三方服务,因此对您的服务的授权不会失败,这可能会有争议,但在那种情况下,您仍然可能不想 return 200
而是 400 Bad Request
因为成功完成 API 操作的参数无效。
我会说它仍然应该是 401。后端的工作应该与应用程序无关,因为它的内部工作对最终用户并不重要。您的身份验证服务告诉您它无效这一事实应该足以让您抛出此错误。
我会选择 400 或 401。
401 可能会产生误导,因为对您的 API 的访问并非未经授权。 (特别是如果您的 api 也需要身份验证)
另一方面,400 可能是最好的:
The 400 (Bad Request) status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error
我会逆势而行,你应该 return 200。
状态码401与HTTP认证有关。 W3C 对状态码有如下说法:
The request requires user authentication. The response MUST include a WWW-Authenticate header field (section 14.47) containing a challenge applicable to the requested resource. The client MAY repeat the request with a suitable Authorization header field (section 14.8). If the request already included Authorization credentials, then the 401 response indicates that authorization has been refused for those credentials. If the 401 response contains the same challenge as the prior response, and the user agent has already attempted authentication at least once, then the user SHOULD be presented the entity that was given in the response, since that entity might include relevant diagnostic information.
(source)
由于您的服务器本身可能不使用 HTTP 身份验证,因此您不会 returning WWW-Authenticate header 挑战,因此您不会遵循此规格正确。您呼叫的第 3 方 API 可能会正确执行此操作,但顺便说一下。您的用户请求了您的页面,而不是直接从第三方 API,他们 有权这样做。您的服务器 not 决定他们不值得有效响应 - 其他人的服务器刚刚告诉您他们的令牌无效。
鉴于此,我会 return 200。请求 已 成功。您的服务器能够return信息表明第三方API调用失败。