为什么在Flask Restful中访问access_token_cookie时,即使jwt_required,get_jwt_identity仍返回None?

问题描述

大家好,我正在使用Flask Restful和Flask Jwt Extended来制作日程表,并尝试使用cookie。我已经成功获取并检索了cookie。我下面有一个端点,要求jwt(jwt_required)可以识别cookie并接受它。但是,当我尝试执行get_jwt_identity()时,它不返回任何内容。请注意,它已经超出了@jwt_required装饰器,所以这不是问题。


资源/创建时间表

@classmethod
    @jwt_required
    def post(cls):
        return {"message": get_jwt_identity()}  # Returns {"message": None}
        schedule_list = []
        wb = Workbook()

        day = request.form.get("day")
        odd_or_even = request.form.get("odd_or_even")

        if odd_or_even not in ["odd","even"] or day not in [
            "monday","tuesday","wednesday","thursday","friday",]:
            return {"message": "Not a valid day identifier"}

        user_id = get_jwt_identity()

        day_lib = ScheduleModel.find_by_id(user_id)

        execute_one = f'schedule_list.extend(day_lib.{day})'
        execute_two = f'schedule_list.extend(day_lib.{odd_or_even})'

        exec(execute_one)
        exec(execute_two)

        ScheduleSheet = wb.add_sheet("Schedule")

        ScheduleSheet.write(0,"Schedule:")

        for row in range(len(schedule_list)):
            ScheduleSheet.write(
                row + 2,schedule_list[row-1].split("> ")[0]
            )
            ScheduleSheet.write(
                row + 2,1,schedule_list[row-1].split("> ")[1]
            )
            ScheduleSheet.write(
                row + 2,2,schedule_list[row-1].split("> ")[2]
            )

        filepath = "static/schedules"
        name = f"{user_id}.xls"

        wb.save(filepath + "/" + name)

        return send_from_directory(filepath,name),{"message": "Your file has Now been downloaded!"}

app.py

app = Flask(__name__)
api = Api(app)
# csrf.init_app(app)

app.config.from_object("default_config")
app.config.from_envvar("APPLICATION_SETTINGS")


@app.before_first_request
def create_Tables():
    db.create_all()


jwt = JWTManager(app)

app.config.from_object("default_config")
app.config.from_envvar("APPLICATION_SETTINGS")

api.add_resource(ScheduleMaker,"/make_schedule")
api.add_resource(GithubLogin,"/login")
api.add_resource(GithubAuthorize,"/login/github/authorized")
api.add_resource(ScheduleMakerRedirect,"/redirect_schedule")

if __name__ == "__main__":
    db.init_app(app)
    ma.init_app(app)
    oauth.init_app(app)
    app.run(port=5000)

default_config.py

import os

DEBUG = True
sqlALCHEMY_DATABASE_URI = "sqlite:///data.db"
sqlALCHEMY_TRACK_MODIFICATIONS = False
SECRET_KEY = os.environ["APP_SECRET_KEY"]
JWT_SECRET_KEY = os.environ["JWT_SECRET_KEY"]
JWT_TOKEN_LOCATION = ["cookies"]
JWT_COOKIE_SECURE = False
JWT_COOKIE_CSRF_PROTECT = False

在此先感谢您,如果您需要其他信息,请告诉我。谢谢! 如果还有另一则问题相同的帖子,请将其发布,然后我将其删除,但是我找不到类似的东西。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)