使用 jwt_required 添加资源?

Jam*_* MV 6 python flask jwt flask-restful

我使用flask创建了一个API,其中使用flask_jwt_extended进行身份验证都可以正常工作。

但是,如果我添加具有 jwt_required 装饰器的资源,则会出现此错误。

  File "/Library/Python/2.7/site-packages/flask_jwt/__init__.py", line 176, in decorator
    _jwt_required(realm or current_app.config['JWT_DEFAULT_REALM'])
KeyError: 'JWT_DEFAULT_REALM'
Run Code Online (Sandbox Code Playgroud)

示例资源:

class Endpoint(Resource):

    @jwt_required()
    def get(self):
        return {"State": "Success"}
Run Code Online (Sandbox Code Playgroud)

初始化应用程序:

app = Flask(__name__)
api = Api(app)
Run Code Online (Sandbox Code Playgroud)

添加资源:

api.add_resource(resource_class, "/myEndpoint")
Run Code Online (Sandbox Code Playgroud)

我让它工作的唯一方法是在与 API 相同的文件中定义 Endpoint 类。

我想我需要某种方式将领域传递到端点类,并使用 jwt_required 上的可选参数来设置领域。

Jam*_* MV 4

发现了这个问题,在我导入的资源中jwt_required

from flask_jwt_extended import jwt_required
Run Code Online (Sandbox Code Playgroud)

但是我需要import jwt_required从 JWT 初始化的类中获取。