【问题标题】:What is the difference between UnauthorizedException vs OAuthRequestException in Cloud Endpoints?Cloud Endpoints 中的 UnauthorizedException 与 OAuthRequestException 有什么区别?
【发布时间】:2014-08-13 15:41:53
【问题描述】:

在 Cloud Endpoints 中,我知道在执行 OAuth 时,我需要检查 user == null 是否确定用户是否已通过身份验证。如果用户为空,我应该抛出异常。在示例 Google Cloud Endpoints 代码 sn-ps 中,我看到使用了两个不同的异常。

OAuth documentation for cloud endpoints 表示要抛出 OAuthRequestException。但是,我看到其他代码库(包括Udacity Course)抛出UnauthorizedException

我注意到 OAuthRequestException 没有从 com.google.api.server.spi.ServiceException 扩展,所以我认为 UnauthorizedException 是正确的选择?

我应该使用哪一个?

【问题讨论】:

    标签: java google-app-engine google-cloud-endpoints


    【解决方案1】:

    我会坚持使用UnauthorizedException' since it extends from theServiceException` 类。根据https://developers.google.com/appengine/docs/java/endpoints/exceptions 和 API 最佳实践中的文档,建议以抛出正确 HTTP 状态代码的方式映射异常。

    因此,在 UnauthorizedException 的情况下,会抛出 HTTP 401。

    这是我通常在我的代码中执行的操作,我相信(您可以尝试!)您会看到一个标准的捕获所有 HTTP 错误代码,以防您抛出像 OAuthRequestException 这样不扩展的异常ServiceException(HTTP 503 或 HTTP 500)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-29
      • 1970-01-01
      • 2019-06-22
      • 2020-06-10
      • 1970-01-01
      • 1970-01-01
      • 2016-10-21
      相关资源
      最近更新 更多