令牌身份验证不适用于 Django Rest 框架
Token Authentication Not Working on Django Rest Framework
我有一个 Django 应用程序,我将 DRF 用于我的 API 会话和令牌身份验证。我安装的应用程序中有 rest_framework 和 rest_framework.authtoken。我已经迁移了我的数据库,可以在 Django 管理中为用户创建令牌。我知道所有这一切都有效,因为我正在访问 rest_framework.auth_token 的 obtain_auth_token 视图,以便在 POST 请求中提交用户数据时返回一个令牌,并收到一个返回。当我尝试向我的应用程序中的视图函数发出 GET 请求时,它的视图集上有 TokenAuthentication,它一直返回。
{"detail":"Authentication credentials were not provided."}
设置文件
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# My Apps
'rest_framework',
'rest_auth',
'rest_framework.authtoken',
]
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.TokenAuthentication',
],
}
网址
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from rest_framework.authtoken import views
from api.views.some_model import MyViewSet
urlpatterns = [
path('', include(router.urls)),
path('rest-auth/', include('rest_auth.urls')),
path('api-token-auth/', views.obtain_auth_token)
]
视图集
from rest_framework.viewsets import ModelViewSet
from rest_framework.authentication import SessionAuthentication, TokenAuthentication
from rest_framework.permissions import IsAuthenticated
from some_app.models import SomeModel
from api.serializers.exams import SomeModelSerializer
class ExamViewSet(ModelViewSet):
permission_classes = (IsAuthenticated,)
authentication_classes = (TokenAuthentication, SessionAuthentication)
queryset = SomeModel.objects.all()
serializer_class = SomeModelSerializer
Python 获取响应的脚本
import requests
import json
data = {
"username": "myemail@gmail.com",
"password": "password124"
}
url = "http://localhost:8002/api/v1/api-token-auth/"
response = requests.post(url, data=data)
token = json.loads(response.text).get('token')
if token:
token = f"Token {token}"
headers = {"Authentication": token}
response = requests.get("http://localhost:8002/api/v1/model/", headers=headers)
print(response.text)
else:
print('No Key')
Header 名称应该是 Authorization
而不是 Authentication
:
headers = {"Authorization": token}
response = requests.get("http://localhost:8002/api/v1/model/", headers=headers)
令牌应在header like
中提供
-H "Authorization: Token 8fa36c01df3bb9ed31fc2329c53a9fe2cac72966"
如果您在 Thunder 客户端上使用 POSTMAN 发出请求,您必须在 Header 中添加键和值,如下所示
KEY:授权
VALUE:令牌 YourToken
For Example Click Here
我有一个 Django 应用程序,我将 DRF 用于我的 API 会话和令牌身份验证。我安装的应用程序中有 rest_framework 和 rest_framework.authtoken。我已经迁移了我的数据库,可以在 Django 管理中为用户创建令牌。我知道所有这一切都有效,因为我正在访问 rest_framework.auth_token 的 obtain_auth_token 视图,以便在 POST 请求中提交用户数据时返回一个令牌,并收到一个返回。当我尝试向我的应用程序中的视图函数发出 GET 请求时,它的视图集上有 TokenAuthentication,它一直返回。
{"detail":"Authentication credentials were not provided."}
设置文件
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# My Apps
'rest_framework',
'rest_auth',
'rest_framework.authtoken',
]
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.TokenAuthentication',
],
}
网址
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from rest_framework.authtoken import views
from api.views.some_model import MyViewSet
urlpatterns = [
path('', include(router.urls)),
path('rest-auth/', include('rest_auth.urls')),
path('api-token-auth/', views.obtain_auth_token)
]
视图集
from rest_framework.viewsets import ModelViewSet
from rest_framework.authentication import SessionAuthentication, TokenAuthentication
from rest_framework.permissions import IsAuthenticated
from some_app.models import SomeModel
from api.serializers.exams import SomeModelSerializer
class ExamViewSet(ModelViewSet):
permission_classes = (IsAuthenticated,)
authentication_classes = (TokenAuthentication, SessionAuthentication)
queryset = SomeModel.objects.all()
serializer_class = SomeModelSerializer
Python 获取响应的脚本
import requests
import json
data = {
"username": "myemail@gmail.com",
"password": "password124"
}
url = "http://localhost:8002/api/v1/api-token-auth/"
response = requests.post(url, data=data)
token = json.loads(response.text).get('token')
if token:
token = f"Token {token}"
headers = {"Authentication": token}
response = requests.get("http://localhost:8002/api/v1/model/", headers=headers)
print(response.text)
else:
print('No Key')
Header 名称应该是 Authorization
而不是 Authentication
:
headers = {"Authorization": token}
response = requests.get("http://localhost:8002/api/v1/model/", headers=headers)
令牌应在header like
中提供 -H "Authorization: Token 8fa36c01df3bb9ed31fc2329c53a9fe2cac72966"
如果您在 Thunder 客户端上使用 POSTMAN 发出请求,您必须在 Header 中添加键和值,如下所示
KEY:授权
VALUE:令牌 YourToken
For Example Click Here