如果未提供令牌,则Flask API返回自定义的NoAuthorizationError

问题描述

我有一个简单的API,该API由使用flask_restx构建的基本Flask API保护。

@api.route("/api/data")
class Index(Resource):
    @jwt_required
    def get(self):
        try:
            username = get_jwt_identity()
            data = SchnittstelleJSON.Get3Objekt()
            data = json.loads(data)
            return {"data": data},200
        except:
            return {"Error": "Retreiving data not possible"},401

与标准"msg": "Missing Authorization Header"相比,我想为最终用户提供更多有用的信息。我试图用try else语句解决该问题,但是它不起作用。我猜可能会发生这种情况,因为装饰器之前已经处理过此问题。有没有办法为最终用户编写自定义错误消息?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)