0
我目前正在研究python-eve庫來創建一個restful API,但是當我按照本教程實現我獲得的「Token Authentication」時遇到了一些問題錯誤401說「請提供適當的憑據」。EVE REST-在python Eve框架中出現AuthToken錯誤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
如果可能的話,請爲我提供工作代碼。我是這個領域的小菜鳥。
謝謝!
我不確定哪一個編碼base64以及如何去做。我有一個收集「用戶名」= ab和「令牌」= 54321 –