内部服务器错误,而不是从Auth0引发AuthError响应



我有一个flask restful app.py,它包含了我所有的主要函数。我已经按照这里的指示创建了一个server.py文件:https://auth0.com/docs/quickstart/backend/python

在服务器上的app.py文件中,我导入AuthError并要求auth。然后,我将@requires_auth放在函数前面。

当我有一个有效的jwt时,它工作得很好。当jwt无效时,它将失败。失败是好事,因为请求不应该起作用。但是,我从api中得到的响应是"Internal Server Error",而不是Server.py文件中raise AuthError部分中的详细响应。

我得到2个错误:

Traceback (most recent call last):
File "C:UsersMEcodeserver.py", line 88, in decorated
issuer="https://"+AUTH0_DOMAIN+"/"
File "C:UsersMElibsite-packagesjosejwt.py", line 150, in decode
options=defaults)
File "C:UsersMElibsite-packagesjosejwt.py", line 457, in _validate_claims
_validate_exp(claims, leeway=leeway)
File "C:UsersMElibsite-packagesjosejwt.py", line 299, in _validate_exp
raise ExpiredSignatureError('Signature has expired.')
jose.exceptions.ExpiredSignatureError: Signature has expired.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:UsersMElibsite-packagesflaskapp.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "C:UsersMElibsite-packagesflaskapp.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:UsersMElibsite-packagesflask_restful__init__.py", line 480, in wrapper
resp = resource(*args, **kwargs)
File "C:UsersMElibsite-packagesflaskviews.py", line 88, in view
return self.dispatch_request(*args, **kwargs)
File "C:UsersMElibsite-packagesflask_restful__init__.py", line 595, in dispatch_request
resp = meth(*args, **kwargs)
File "C:UsersMEcodeserver.py", line 92, in decorated
"description": "token is expired"}, 401)
server.AuthError: ({'code': 'token_expired', 'description': 'token is expired'}, 401)

如何获得AuthError作为对调用的响应,而不仅仅是我的内部服务器错误?

谢谢!

Auth0中的此特定教程存在问题,它指示您在auth.py:中包含错误处理程序

@app.errorhandler(AuthError)
def handle_auth_error(ex):
response = jsonify(ex.error)
response.status_code = ex.status_code
return response

相反,您必须将此处理程序包含在app.py中,实际使用的是@requires_auth
请注意,要执行此操作,您需要添加相关导入:

from auth import AuthError
from flask import jsonify

注意:为了能够从auth.py导入,您需要在同一目录中添加一个空文件__init__.py

尝试设置app.config[“PROPAGATE_EXCEPTIONS”] = True

Uou可以使用错误处理程序来显式捕获这些错误,并基于它们返回一些显式json。

相关内容

  • 没有找到相关文章

最新更新