cannot import name 'token_required'
时间: 2023-10-14 15:06:30 浏览: 76
This error typically occurs when you are trying to use a function or decorator called `token_required` but it cannot be found in the current scope or module.
If you are trying to use the `token_required` decorator from the Flask-RESTful library, you may need to make sure that you have imported it correctly. Here is an example of how to properly import and use `token_required` in a Flask-RESTful application:
```
from flask import Flask
from flask_restful import Api, Resource, reqparse
from flask_jwt_extended import jwt_required, get_jwt_identity
from your_module import token_required
app = Flask(__name__)
api = Api(app)
class SecretResource(Resource):
@jwt_required
@token_required
def get(self):
current_user = get_jwt_identity()
return {'secret': 'Hello, {}! This is a secret message.'.format(current_user)}
api.add_resource(SecretResource, '/secret')
if __name__ == '__main__':
app.run(debug=True)
```
In this example, we have imported `token_required` from `your_module` and used it as a decorator alongside `jwt_required` to protect a resource endpoint. Make sure that the `token_required` function is defined and imported correctly in your module.
阅读全文