Subhayan Bhattacharya
Subhayan Bhattacharya

Reputation: 5705

Unable to get json web token from Flask

I have the below code for app.py:

from flask import Flask
from flask_jwt import JWT, jwt_required, current_identity
from werkzeug.security import safe_str_cmp

class User(object):
    def __init__(self, id, username, password):
        self.id = id
        self.username = username
        self.password = password

    def __str__(self):
        return "User(id='%s')" % self.id

users = [
    User(1, 'user1', 'abcxyz'),
    User(2, 'user2', 'abcxyz'),
]

username_table = {u.username: u for u in users}
userid_table = {u.id: u for u in users}

def authenticate(username, password):
    user = username_table.get(username, None)
    if user and safe_str_cmp(user.password.encode('utf-8'), password.encode('utf-8')):
        return user

def identity(payload):
    user_id = payload['identity']
    return userid_table.get(user_id, None)

app = Flask(__name__)
app.debug = True
app.config['SECRET_KEY'] = 'super-secret'

jwt = JWT(app, authenticate, identity)

@app.route('/protected')
@jwt_required()
def protected():
    return '%s' % current_identity

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

When i try to make a post request to get the authentication token from Postman : http://127.0.0.1:5000/auth

{ "username": "joe", "password": "pass" }

I get a peculiar error which i am not able to solve :

(flask-restful) C:\Users\bhatsubh\Projects\flask-restful>python Test.py
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 973-755-004
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [06/Sep/2017 13:38:08] "POST /auth HTTP/1.1" 500 -
Traceback (most recent call last):
  File "C:\Users\bhatsubh\Envs\flask-restful\lib\site-packages\flask\app.py", li
ne 1997, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Users\bhatsubh\Envs\flask-restful\lib\site-packages\flask\app.py", li
ne 1985, in wsgi_app
    response = self.handle_exception(e)
  File "C:\Users\bhatsubh\Envs\flask-restful\lib\site-packages\flask\app.py", li
ne 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\bhatsubh\Envs\flask-restful\lib\site-packages\flask\_compat.py"
, line 33, in reraise
    raise value
  File "C:\Users\bhatsubh\Envs\flask-restful\lib\site-packages\flask\app.py", li
ne 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\bhatsubh\Envs\flask-restful\lib\site-packages\flask\app.py", li
ne 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\bhatsubh\Envs\flask-restful\lib\site-packages\flask\app.py", li
ne 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\bhatsubh\Envs\flask-restful\lib\site-packages\flask\_compat.py"
, line 33, in reraise
    raise value
  File "C:\Users\bhatsubh\Envs\flask-restful\lib\site-packages\flask\app.py", li
ne 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\bhatsubh\Envs\flask-restful\lib\site-packages\flask\app.py", li
ne 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Users\bhatsubh\Envs\flask-restful\lib\site-packages\flask_jwt\__init_
_.py", line 115, in _default_auth_request_handler
    username = data.get(current_app.config.get('JWT_AUTH_USERNAME_KEY'), None)
AttributeError: 'NoneType' object has no attribute 'get'

I have absolutely no idea what is going wrong. Can someone please help me with this?

Upvotes: 3

Views: 2388

Answers (4)

Nikunj
Nikunj

Reputation: 1

try this: localhost:5000/auth

{ "username": "user1", "password": "abcxyz" }

Upvotes: 0

Brian Cugelman
Brian Cugelman

Reputation: 184

I suspect you’re being thrown off by this because there’s a bug in flask-jwt which is throwing up errors rather than providing proper feedback. So it’s hard to know what’s up.

If there are any header errors, it triggers errors rather than a proper response.

Dave B’s suggestion worked for me, setting the header with: Content-Type:application/json

I raised this with the developers, so hopefully, they can patch it up.

Upvotes: 2

Dave
Dave

Reputation: 524

make sure have set correct header

Content-Type:application/json

try curl for cross check

$ curl -H "Content-Type: application/json" -X POST -d '{ "username": "joe", "password": "pass" }' http://localhost:5000/auth

Upvotes: 6

vimalloc
vimalloc

Reputation: 4167

Look at https://github.com/vimalloc/flask-jwt-extended or https://github.com/vimalloc/flask-jwt-simple as alternatives to Flask-JWT. They are better designed, up to date, and still maintained (I am the author, so I am of course biased).

Upvotes: 0

Related Questions