flask-restx icon indicating copy to clipboard operation
flask-restx copied to clipboard

Working with flask-jwt-extended and flask-restx

Open perymerdeka opened this issue 3 years ago • 1 comments

Ask a question

I'm building an API with Flask-Restx, and I'm using flask-jwt-extended for the JSON web Token, I have views like this

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"])

and here is my config file

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")

then an error like this appears when I run it using docker and gunicorn

flask_jwt_extended.exceptions.NoAuthorizationError: Missing JWT in headers or cookies (Missing Authorization Header; Missing cookie "access_token_cookie")

I'm using Postman to test the API that I've created,

authorization

header

how do I solve the problem above? Can anyone help?

perymerdeka avatar Aug 21 '22 06:08 perymerdeka

This sounds like it's maybe related to https://github.com/vimalloc/flask-jwt-extended/issues/86#issuecomment-335509456 ?

peter-doggart avatar Aug 22 '22 09:08 peter-doggart

Looks like you missed Authorization header in your request. Try adding Authorization: Bearer {your_jwt} to your request headers

YUMEYA avatar Sep 30 '22 02:09 YUMEYA