无法在Flask JWT Extended中获取授权标头

2024-09-29 20:22:51 发布

您现在位置:Python中文网/ 问答频道 /正文

目前,我正在尝试使用Flask JWT扩展到基本级别。 我从用户表单中获取经过身份验证的用户和密码。我创建了一个access令牌,将其包含在响应中并路由到另一个受保护的路由。请查找以下代码的较短版本

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 但是再好不过了。 请让我知道这个问题的任何解决方案。 对不起,如果有任何打字错误

感谢和问候, 阿比奈J K


Tags: formtokenlogconfigappflaskaccessrequest
2条回答

根据您使用的版本,根据最新稳定版本的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

如果您使用邮递员发送请求,请确保选中“密钥”

相关问题 更多 >

    热门问题