English 中文(简体)
如果第三党API认证失败, 我的 API 会返回什么 HTTP 错误代码?
原标题:What HTTP error codes should my API return if a 3rd party API auth fails?

我在撰写一份REST-ish API 服务,它提供通过OAuth与其他第三方服务(他们自己REST APIs)的最终用户数据进行互动的能力。 一个常见的例子可能是从我的服务向Facebook或Twitter等第三方服务公布数据。

比如说,我与终端用户和脸书进行了OAUT舞蹈,结果产生了一些短期访问信号,我的服务可以用来与用户的Facebook账户互动。 如果访问信号过期,用户试图利用我的服务向Facebook发布,那么我又会回到用户身上什么错误?

401在我看来并不完全正确; 401似乎将适用于有我服务的用户的指定状态。 403似乎更合适,但也相当普通。

最佳回答

401是前进的道路。 RFC2616的两段节选确定了HTTP协议:

第10.4.2节(约401节):

If the request already included Authorization credentials, then the 401 response indicates that authorization has been refused for those credentials.

这似乎适用于过期的标记。 有认证证书, 但被拒绝, 因此用户代理必须重新认证 。

第10.4.4节(约403节):

The server understood the request, but is refusing to fulfill it. Authorization will not help and the request SHOULD NOT be repeated.

当资源尽管有用户证书却无法访问时,就应该使用这一方法。 这可能是一个网站/API,它只能对被亚洲IP或被宣布为有害且被禁用的网页击中的美国起作用(因此找到了内容,但服务器拒绝为内容服务 ) 。

在 OAuth2 上,推荐的工作流程取决于代号是如何传递的。如果由 认证 信头通过, 服务器可以返回一个 401 。 当通过查询字符串参数通过时, 最适当的响应是 400 个错误请求( 不幸地, 最通用的 HTTP 请求) 。 由 OAuth2 pec < a href=" https://datatracker. ietf.org/doc/ html/ forf- oauth- v2-26" rel= “ nofollown noreferrer” > https://datatrader. ietf.org/doc/html/for- ietf-oauth- v2-26 < /a> 所定义。

问题回答

通用无错, 听起来像403个状态是相关的- 没有什么能阻止你提供更人性化的可读版本, 更详细地解释为什么。

我认为,如果在错误答复方面你具有某种程度的雄心,那么以下是一份全面的清单。

<%% 1 > 坏请求

对于出现错误格式化的请求, 例如, 如果参数需要 0- 9 和 11 之间的一英寸, 已经发送 。 您可以返回此请求, 在响应体中指定 参数 x 要求值在 0 和 9 之间 。

<% 1> > 401 未经授权

仅用于授权问题 。 签名可能是错误的, 可能以前曾经使用过, 发送的时间戳不在可接受的时间窗口内, 再次使用响应机构来更确切地说明您为什么使用它来回应。 为了澄清使用它的目的, 仅用于与 OAUT 相关的错误 。

<%t>403 禁禁

明示表示一个完善且得到授权的操作根本不可能( 无论是现在还是将来) 。 例如, 如果资源已被另一个用户锁定编辑 : 使用响应体按 < code> 的写法说些什么, 其他人正在编辑, 您将不得不等待 mmkay?

403 禁止 也可能与试图获取资源有关。 例如,用户可以访问资源/ 资源/ 101212/ propertys.json, 但不能访问 /resources/999/ propertys.json, 那么您可以简单地说 : 由于访问权 在响应机中被禁止 。

<%t>404 找不到

请求的资源不存在。 或者 URL 无法成功映射到您服务中的 API 。 请在响应框中指定 。

<强度>405 方法不允许

这是要表示无法调用 API, 例如 GET 无法调用, 但必须使用另一种方法 。 在此返回时, 您还需要返回额外的响应头 < code> Allow: POST, PUT 等





相关问题
How to set response filename without forcing "save as" dialog

I am returning a stream in some response setting the appropriate content-type header. The behavior I m looking for is this: If the browser is able to render content of the given content type then it ...

Which Http redirects status code to use?

friendfeed.com uses 302. bit.ly uses 301. I had decided to use 303. Do they behave differently in terms of support by browsers ?

Does HttpWebRequest send 200 OK automatically?

Background: I am implementing Paypal IPN handler. This great article on Paypal states that I am required to send a 200 OK back to Paypal after I read the response. The processing of IPN request is ...

Java HTTPAUTH

我试图把桌面应用程序连接起来,我是同D.icio.us api @ Delicious Alan书写的,简单地向他们提供我的用户名和密码,并请他把书记上写给我......。

Finding out where curl was redirected

I m using curl to make php send an http request to some website somewhere and have set CURLOPT_FOLLOWLOCATION to 1 so that it follows redirects. How then, can I find out where it was eventually ...