繁体   English   中英

Cloud Endpoints中UnauthorizedException与OAuthRequestException有什么区别?

[英]What is the difference between UnauthorizedException vs OAuthRequestException in Cloud Endpoints?

在Cloud Endpoints中,我了解在执行OAuth时,我需要检查user == null以确定用户是否已经过身份验证。 在用户为null的情况下,我应该抛出异常。 在Google Cloud Endpoints示例代码段中,我看到了两种不同的例外情况。

云端点OAuth文档说要抛出OAuthRequestException 但是,我已经看到其他代码库(包括Udacity课程 )抛出UnauthorizedException

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

我应该使用哪一个?

我会坚持UnauthorizedException' since it extends from the ServiceException`类UnauthorizedException' since it extends from the 根据https://developers.google.com/appengine/docs/java/endpoints/exceptions和API Best Practices中的文档,建议以引发正确的HTTP状态代码的方式映射异常。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM