Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 401尝试在框架中对用户进行身份验证时_Python_Authentication_Flask_Jwt_Eve - Fatal编程技术网

Python 401尝试在框架中对用户进行身份验证时

Python 401尝试在框架中对用户进行身份验证时,python,authentication,flask,jwt,eve,Python,Authentication,Flask,Jwt,Eve,我使用awesome创建了一个带有JWT身份验证的crudapi。我已经看过了教程,但是在对需要令牌身份验证的端点执行POST请求时,我收到了一个401错误 我读过这个问题:但是我很确定这个令牌是Base64编码的 这是我在执行cURL GET请求时从服务器返回的响应: curl -H "Authorization: <MYTOKEN>" -i http://MY_IP/users/548f6ecd64e6d12236c9576b ---- Response ---- HTTP/

我使用awesome创建了一个带有JWT身份验证的crudapi。我已经看过了教程,但是在对需要令牌身份验证的端点执行POST请求时,我收到了一个401错误

我读过这个问题:但是我很确定这个令牌是Base64编码的

这是我在执行cURL GET请求时从服务器返回的响应:

curl -H "Authorization: <MYTOKEN>" -i http://MY_IP/users/548f6ecd64e6d12236c9576b

---- Response ----

HTTP/1.1 401 UNAUTHORIZED
Server: nginx/1.4.6 (Ubuntu)
Date: Tue, 16 Dec 2014 10:49:25 GMT
Content-Type: application/json
Content-Length: 91
Connection: keep-alive
WWW-Authenticate: Basic realm:"eve"

{"_status": "ERR", "_error": {"message": "Please provide proper credentials", "code": 401}}
设置.py

我在我的MongoDB中为用户存储了一个令牌,我正在使用该令牌发出请求,并将该令牌包含在授权标头中,如下所示:

Authorization: <USERTOKEN> 
有没有想过为什么这会给我401


谢谢

我测试了您的代码,但为了简单起见,我去掉了jwt.encode:

我向/users发布了一个新用户,然后与Postman实现了同一个端点:它就像一个charm 200。然后我对curl做了同样的测试:

这也重新计算了一个200。正如您所看到的,Auth头是编码的,从Postman请求预览复制粘贴。只需确保您传递了正确的内容,可以在check_auth中设置一个断点来验证其中的内容,以及db查找是否成功

希望这有助于诊断您的问题

PS:请注意,在curl中,Authorization头在token之前还有一个Basic语句,这似乎是您的代码片段中缺少的

更新

为了它,我还安装了PyJWT并测试了您的代码和。。。在我这方面效果很好。你的要求一定有问题

更新2


有一件事情可能不是很明显,那就是您仍然需要添加一个编码的:在您的auth头中,它是基本auth,并且它解析username和pw。这就是为什么在上面的示例中,在编码的“hello”末尾有a=的原因

谢谢你的回复!因此,基本上,身份验证服务解码我在授权头中发送的令牌?您是否也可以发布您对PyJWT的实现。我不确定我做得对。再次感谢。啊,我现在明白了。问题解决了:谢谢你花时间。我想@nicola iarocci的意思是:不;所以要编码的字符串应该是hello:@HenriqueB。谢谢你指出这一点。我编辑了尼古拉的答案,并更改了答案;致:。
users_schema = {
    'username': {
        'type': 'string',
        'required': True,
    },
    'password': {
        'type': 'string',
        'required': True,
    },
    'email': {
        'type': 'string',
        'minlength': 1,
        'maxlength': 200,
        'required': True,
    },
    'token': {
        'type': 'string',
    },
    'created': {
        'type': 'datetime',
    }
}

users = {
    'cache_control': '',
    'cache_expires': 0,
    'extra_response_fields': ['token'],
    'public_methods': ['POST'],
    'schema': users_schema
}

DOMAIN = {
    'users': users,
}
Authorization: <USERTOKEN> 
def add_token(documents):
    for document in documents:
        payload = {'username': document['username']}
        document["token"] = 'hello'
curl -H "Authorization: Basic Y2lhbzo=" -i http://localhost:5000/users