无法从Flask获取json网络令牌

时间:2017-09-06 08:14:36

标签: python flask flask-restful

我有以下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()

当我尝试发帖子请求从Postman获取身份验证令牌时: http://127.0.0.1:5000/auth

{     “用户名”:“乔”,     “密码”:“通过” }

我得到了一个我无法解决的特殊错误:

(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'

我完全不知道出了什么问题。有人可以帮帮我吗?

4 个答案:

答案 0 :(得分:3)

确保设置了正确的标题

内容类型:应用/ JSON

尝试卷曲进行交叉检查

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

答案 1 :(得分:0)

https://github.com/vimalloc/flask-jwt-extendedhttps://github.com/vimalloc/flask-jwt-simple视为Flask-JWT的替代品。它们设计得更好,最新,并且仍然保持着(我是作者,所以我当然有偏见)。

答案 2 :(得分:0)

我怀疑您被这个问题吓倒了,因为flask-jwt中存在一个错误,该错误引发了错误,而不是提供适当的反馈。因此,很难知道发生了什么。

如果有任何标头错误,它将触发错误而不是适当的响应。

Dave B的建议对我有用,将标题设置为:Content-Type:application / json

我向开发人员提出了这一建议,因此希望他们可以对其进行修补。

答案 3 :(得分:0)

试试这个: 本地主机:5000/认证

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