我目前正在开发python-eve库来创建restful API,但是当我按照本教程实现“令牌身份验证”时遇到了一些问题,我收到错误401,说“请提供正确的凭据”。
下面是我的用户模式:
RESOURCE_METHODS = ['GET', 'POST']
ITEM_METHODS = ['GET','PATCH','DELETE']
DOMAIN = {
'user': {
'additional_lookup': {
'url': 'regex("[\w]+")',
'field': 'username',
#'url': '[\w]+',
},
'schema': {
'firstname': {
'type': 'string'
},
'lastname': {
'type': 'string'
},
'phone': {
'type': 'string'
},
'username': {
'type': 'string',
'required': True,
'unique': True,
},
'password': {
'type': 'string',
'required': True,
},
'roles': {
'type': 'list',
'allowed': ['user', 'superuser', 'admin'],
'required': True,
},
'token': {
'type': 'string',
'required': True,
}
},
'cache_control': '',
'cache_expires': 0,
'allowed_roles': ['superuser', 'admin'],
},
'item': {
'schema': {
'name':{
'type': 'string'
},
'username': {
'type': 'string'
}
}
},}
这是我的app.py
from eve import Eve
from eve.auth import TokenAuth
import random
import string
class RolesAuth(TokenAuth):
def check_auth(self, token, allowed_roles, resource, method):
accounts = app.data.driver.db['eve']
lookup = {'token': token}
if allowed_roles:
lookup['roles'] = {'$in': allowed_roles}
account = accounts.find_one(lookup)
return account
def add_token(documents):
for document in documents:
document["token"] = (''.join(random.choice(string.ascii_uppercase)
for x in range(10)))
app = Eve(settings='settings.py')
if __name__ == '__main__':
app = Eve(auth=RolesAuth)
app.on_insert_accounts += add_token
app.run()你知道为什么我会得401分吗?
我正在使用python 3.4
如果可能,请为我提供工作代码。我是这个领域的新手。
谢谢!
发布于 2015-07-30 15:17:29
您需要对令牌进行如下编码:
echo "54321:" | base64请不要忘记上一个:
因为您直接查找token (根据您的代码),所以不需要username。
https://stackoverflow.com/questions/30998900
复制相似问题