如何使用Rest Framework的默认令牌身份验证视图?

问题描述

我正在尝试使用DRF的内置TokenAuthentication视图,但无法正常工作。

这是测试:

import requests
import json
data = json.dumps({
  'username': 'test-sandbox-root','password': 'password'
})
headers = {'Content-Type': 'application/json'}
r = requests.post('http://localhost:8000/api/auth',data=data,headers=headers)
>>> r.status_code
400
>>> r.text
'{"non_field_errors":["Unable to log in with provided credentials."]}'

但是用户显然存在:

>>> User.objects.get(username='test-sandbox-root',password='password')
<User: test-sandbox-root>

我已将 DEFAULT_AUTHENTICATION_CLASSES 添加到我的设置中:

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework.authentication.BasicAuthentication','rest_framework.authentication.SessionAuthentication','rest_framework.authentication.TokenAuthentication',]
}

并迁移了 rest_framework.authtoken 应用:

INSTALLED_APPS = [
    ...

    'rest_framework','rest_framework.authtoken',...
]

并将 optain_auth_token 添加到我的 urls.py

from rest_framework.authtoken import views

urlpatterns = [
    path('/api/auth',views.obtain_auth_token),]

我想念什么?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...