[英]Working with flask-jwt-extended and flask-restx for Headers and Cookie Token
我正在使用 Flask-Restx 構建一個 API,並且我正在為 JSON web 令牌使用flask-jwt-extended,我有這樣的觀點
class InputReaderView(Resource):
"""Endpoint for validate args from PMR Response with XSD and Validate DataType from EfileType.json
Args:
Resource (_type_): _description_
Returns:
_type_: valid or Invalid Field
"""
@jwt_required()
def post(self):
input_reader = reqparse.RequestParser()
input_reader.add_argument("type", type=str, required=True)
input_reader.add_argument("data", type=dict, action="append", required=True)
args = input_reader.parse_args()
xsd_path = os.path.join(BaseConfig.UPLOAD_FOLDER, "xsd")
for file in os.listdir(xsd_path):
if file.replace(".json", "") == args["type"]:
efile_path = os.path.join(
os.path.join(BaseConfig.UPLOAD_FOLDER, "efile"), "efileTypes.json"
)
validator = validate_schedule(
efile=efile_path,
xsd_schedule=xsd_path,
args=args["data"],
)
if validator["valid status"] == True:
return jsonify(validator["data"], 200)
else:
return abort(400, validator["data"])
這是我的配置文件
class BaseConfig:
BASE_DIR = Path(__file__).resolve().parent.parent
SECRET_KEY = "TheSecretKey"
SQLALCHEMY_TRACK_MODIFICATIONS = True
UPLOAD_FOLDER = os.path.join(BASE_DIR, 'media')
# JSON Web Token Configuration
JWT_SECRET_KEY = "The JWT Secret Key"
JWT_TOKEN_LOCATION = ["headers", "cookies"]
JWT_ACCESS_TOKEN_EXPIRES = timedelta(days=1)
JWT_COOKIE_SECURE = True
JWT_REFRESH_TOKEN_EXPIRES = timedelta(days=7)
class DevelopmentConfig(BaseConfig):
SQLALCHEMY_DATABASE_URI = "postgresql://postgres:1234@localhost:5432/ecommerce_db"
# JSON Web Token Configuration
JWT_SECRET_KEY = "The JWT Secret Key"
JWT_COOKIE_SECURE = True
FLASK_ENV= "development"
class ProductionConfig(BaseConfig):
SQLALCHEMY_DATABASE_URI = os.environ.get("DATABASE_URI")
SQLALCHEMY_TRACK_MODIFICATIONS = True
FLASK_ENV= os.environ.get("FLASK_ENV")
# JWT Configuration
JWT_SECRET_KEY = os.environ.get("JWT_SECRET_KEY")
JWT_COOKIE_SECURE = os.environ.get("JWT_COOKIE_SECURE")
然后當我使用 docker 和 gunicorn 運行它時會出現這樣的錯誤
flask_jwt_extended.exceptions.NoAuthorizationError: Missing JWT in headers or cookies (Missing Authorization Header; Missing cookie "access_token_cookie")
我正在使用 Postman 來測試我創建的 API,
我該如何解決上述問題? 任何人都可以幫忙嗎?
github 問題: https://github.com/python-restx/flask-restx/issues/467
這意味着您的應用程序沒有收到由 postman 生成的令牌,您是否嘗試在標題部分添加一個單元格,名稱為“授權”,您的令牌在“Barear”之后作為值? 或者在cookie中添加,比如“access_token_cookie='Barear your_token'”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.