Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flask_jwt_extended.exceptions.NoAuthorizationError: Missing Authorization Header #251

Closed
wisp888 opened this issue Jun 19, 2019 · 7 comments

Comments

@wisp888
Copy link

wisp888 commented Jun 19, 2019

from flask import Flask
from flask_jwt_extended import JWTManager, jwt_required
from flask_restful import Api, Resource

app = Flask(__name__)
app.config['PROPAGATE_EXCEPTIONS'] = True

api = Api(app)
jwt = JWTManager(app)


class SomeRoute(Resource):
    @jwt_required
    def post(self):
        return {'message':'success'}, 200


api.add_resource(SomeRoute, '/some_route', endpoint='some_route')

if __name__ == '__main__':
    app.run(debug=False)

post the url without token or token expired
use flask_restful no problem!

but use flask_restplus it will raise exception

from flask import Flask
from flask_jwt_extended import JWTManager, jwt_required
from flask_restplus import Api, Resource

app = Flask(__name__)
app.config['PROPAGATE_EXCEPTIONS'] = True

api = Api(app)
jwt = JWTManager(app)


class SomeRoute(Resource):
    @jwt_required
    def post(self):
        return {'message':'success'}, 200


api.add_resource(SomeRoute, '/some_route', endpoint='some_route')

if __name__ == '__main__':
    app.run(debug=False)

it will return

.......
    raise NoAuthorizationError(errors[0])
flask_jwt_extended.exceptions.NoAuthorizationError: Missing Authorization Header
@vimalloc
Copy link
Owner

Flask-Restplus has a bug where native flask error handlers don’t work. Hopefully they will fix that eventually, but you should be able to use this as a work around in the mean time: #86 (comment)

@wisp888
Copy link
Author

wisp888 commented Jun 19, 2019

Flask-Restplus has a bug where native flask error handlers don’t work. Hopefully they will fix that eventually, but you should be able to use this as a work around in the mean time: #86 (comment)

thanks,it work!

@vimalloc
Copy link
Owner

Glad to hear it 👍

@aniketsnv-1997
Copy link

aniketsnv-1997 commented Aug 30, 2020

`from flask import Flask
from flask_restful import Api
from flask_sqlalchemy import SQLAlchemy
from flask_jwt_extended import JWTManager
from flask_migrate import Migrate

app = Flask(name)
app.config["SQLALCHEMY_DATABASE_URI"] = "mysql://root:@localhost/test"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.config["PROPAGATE_EXCEPTIONS"] = True

app.secret_key = "aniket"
app.config["JWT_SECRET_KEY"] = "aniket"
app.config["JWT_AUTH_URL_RULE"] = "/login"
ACCESS_EXPIRES = timedelta(minutes=15)
REFRESH_EXPIRES = timedelta(days=30)
app.config['JWT_ACCESS_TOKEN_EXPIRES'] = ACCESS_EXPIRES
app.config['JWT_REFRESH_TOKEN_EXPIRES'] = REFRESH_EXPIRES
app.config["JWT_BLACKLIST_ENABLED"] = True
app.config["JWT_BLACKLIST_TOKEN_CHECKS"] = ["access", "refresh"]
app.config["JWT_TOKEN_LOCATION"] = ['headers']
app.config["JWT_HEADER_NAME"] = 'Authorization'
app.config["JWT_HEADER_TYPE"] = 'Bearer'

db = SQLAlchemy(app)
jwt = JWTManager(app)
api = Api(app)
migrate = Migrate(app, db)

jwt._set_error_handler_callbacks(api)`

@vimalloc @wisp888 I tried using the turnaround hack mentioned above. But I keep getting the No Authorization Error error.
Also, the IDE - PyCharm keeps on suggesting to me that through this line jwt._set_error_handler_callbacks(api), I am trying to access a protected member. Can you please suggest me a way out?

@vimalloc
Copy link
Owner

@aniketsnv-1997 jwt._set_error_handler_callbacks(api) is a work around for flask-restplus. It looks like you are using flask-restful, so that shouldn't apply here. Take a look #86 for some more details

@aniketsnv-1997
Copy link

@vimalloc I did try to use flask_restplus instead of flask_restful. However, whenever I go to the default route of my application, which is http://127.0.0.1:5000/, I get the result displayed in the screenshot below

image

Whereas, when I use flask-restful, my default route works fine as expected -
image

@aniketsnv-1997
Copy link

Also, when I visit the api http://127.0.0.1:5000/add-a-new-user while using flask_restplus, I get the error as TypeError: ShowUsersForm() takes no arguments. However, I am not passing any parameter to the api. The same route works fine while using flask_restful

The resource code is
`
class ShowUsersForm(Resource):
def get(self):
# current_user = UsersModel.find_by_email_address(get_jwt_identity()).name

    headers = {'Content-Type': 'text/html'}
    return make_response(render_template("./users/forms/add_user.html",
                                         title="Add User",
                                         current_user="current_user",
                                         roles=RolesModel.get_all_roles(),
                                         projects=ProjectsModel.get_all_projects(),
                                         rights=RightsModel.get_all_rights()
                                         )
                         , 200, headers)

`

As a result, I am deadlocked between flask_restful and flask_restplus. ANy help upon this will be much appreciated!

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants