简体   繁体   English

无法在 Flask JWT 中获得授权 Header

[英]Unable to get Authorization Header in Flask JWT Extended

I am trying to using to use Flask-JWT extended at the basic level at the moment.我目前正在尝试使用在基本级别扩展的 Flask-JWT。 I get the user and password authenticated from the user form.我从用户表单中获得了经过身份验证的用户和密码。 I create an accesss token, include it in the response and route to another protected route.我创建了一个访问令牌,将其包含在响应中并路由到另一个受保护的路由。 Please find the shorter version of code as below...请找到以下代码的较短版本...

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

It gives me the error as below...它给了我如下错误...

 {"msg":"Missing Authorization Header"}

I tried the answers on this page...https://stackoverflow.com/questions/52087743/flask-restful-noauthorizationerror-missing-authorization-header But couldnt get better.我尝试了此页面上的答案... https://stackoverflow.com/questions/52087743/flask-restful-noauthorizationerror-missing-authorization-header 但无法变得更好。 Please let me know any solution for this issue.请让我知道此问题的任何解决方案。 Sorry if any typo mistake.抱歉,如果有任何拼写错误。

Thanks and regards, Abhinay JK谢谢和问候, Abhinay JK

Depending on the version you are using, accordind to change log of latest stable version, you should be using notation like:根据您使用的版本,根据更改最新稳定版本的日志,您应该使用如下符号:

@log_blueprint.route('/protected', methods=["POST","GET"])
@jwt_required()
def protected():
    current_user = get_jwt_identity()
    return jsonify(logged_in_as=current_user), 200

If you are using postman to send request, make sure you check the "key".如果您使用 postman 发送请求,请务必检查“密钥”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM