目前,我正在尝试使用在基本级别扩展的Flask-JWT。 我从用户表单中获得了验证的用户名和密码。我创建一个访问令牌,将其包含在响应中并路由到另一条受保护的路由。请找到以下较短的代码版本...
from flask import Flask, jsonify, request
from flask_jwt_extended import,JWTManager, jwt_required, create_access_token,get_jwt_identity)
app.config['JWT_SECRET_KEY'] = 'super-secret'
jwt = JWTManager(app)
app.config['JWT_TOKEN_LOCATION'] = ['headers']
app.config['JWT_BLACKLIST_ENABLED'] = True
jwt = JWTManager(app)
app.config['PROPAGATE_EXCEPTIONS'] = True
@log_blueprint.route('/', methods=['GET', 'POST'])
def login():
form = LoginForm()
if request.method == 'POST':
if error is None and username = form.request['user'] and pwd = form.request['pwd'] :
access_token = create_access_token(identity=user)
resp = redirect(url_for('log_blueprint.protected'),access_token)
resp.headers = {'Authorization': 'Bearer {}'.format(access_token)}
return resp
@log_blueprint.route('/protected', methods=["POST","GET"])
@jwt_required
def protected():
current_user = get_jwt_identity()
return jsonify(logged_in_as=current_user), 200
它给我以下错误...
{"msg":"Missing Authorization Header"}
我尝试过此页面上的答案... https://stackoverflow.com/questions/52087743/flask-restful-noauthorizationerror-missing-authorization-header 但是无法变得更好。 请让我知道此问题的任何解决方案。 抱歉,如果有错字。
感谢和问候, Abhinay J K
答案 0 :(得分:0)
如果您使用邮递员发送请求,请确保选中“密钥”。
答案 1 :(得分:0)
根据您使用的版本,根据最新稳定版本的 change log,您应该使用如下表示法:
@log_blueprint.route('/protected', methods=["POST","GET"])
@jwt_required()
def protected():
current_user = get_jwt_identity()
return jsonify(logged_in_as=current_user), 200