pythonflaskjwtflask-restful

Adding resources with jwt_required?


I've created an API using flask, where the authentication is all working fine using flask_jwt_extended.

However if I add a resource that has a jwt_required decorator I get this error.

  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'

Example resource:

class Endpoint(Resource):

    @jwt_required()
    def get(self):
        return {"State": "Success"}

Initialising the app:

app = Flask(__name__)
api = Api(app)

Adding the resource:

api.add_resource(resource_class, "/myEndpoint")

The only way I can get it to work is to define the Endpoint class in the same file as the API.

I think I need someway to pass the Realm into the endpoint class and have use the optional parameter on jwt_required to set the Realm.


Solution

  • Discovered the issue, in the resource I was importing the jwt_required:

    from flask_jwt_extended import jwt_required
    

    However I needed to import jwt_required from the class that where JWT was initalized.