如何解决如何使用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 举报,一经查实,本站将立刻删除。