【问题标题】:Token Authentication Not Working on Django Rest Framework令牌身份验证在 Django Rest 框架上不起作用
【发布时间】:2019-02-20 21:56:36
【问题描述】:

我有一个 Django 应用程序,我将 DRF 用于我的带有 Session 和 Token 身份验证的 API。我安装的应用程序中有rest_framework 和rest_framework.authtoken。我已经迁移了我的数据库,并且可以在 Django Admin 中为用户创建令牌。我知道所有这些都有效,因为我正在访问 rest_framework.auth_token 的 gain_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')

【问题讨论】:

    标签: python django django-rest-framework http-token-authentication


    【解决方案1】:

    标题名称应该是Authorization 而不是Authentication

    headers = {"Authorization": token}
    response = requests.get("http://localhost:8002/api/v1/model/", headers=headers)
    

    【讨论】:

    • 窝窝窝窝。菜鸟失误,好尴尬哈。非常感谢 neverwalkaloner。
    • 我刚刚落入了同一个陷阱。如果您花费数小时开发启用“身份验证”的视图,则很容易犯此错误。
    【解决方案2】:

    令牌应在标头中提供,如

     -H  "Authorization: Token 8fa36c01df3bb9ed31fc2329c53a9fe2cac72966"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-16
      • 2015-07-21
      • 2015-12-15
      • 2014-04-14
      • 2019-07-11
      • 1970-01-01
      • 2018-12-15
      • 1970-01-01
      相关资源
      最近更新 更多