Django:Rest Framework验证头

Pro*_*eus 42 django django-rest-framework

使用Django REST API,我正在尝试验证我的请求.

这就是我要发送的内容:

Content-Type: application/json, Authentication: token="6d82549b48a8b079f618ee9c51a6dfb59c7e2196"
Run Code Online (Sandbox Code Playgroud)

这就是我得到的回报:

{"detail": "Authentication credentials were not provided."}
Run Code Online (Sandbox Code Playgroud)

有人能给我正确的标题吗?

谢谢

标题:

Accept: application/json
Content-Type: application/json
Authorization: Token 6d82549b48a8b079f618ee9c51a6dfb59c7e2196
Connection: keep-alive
Origin: chrome-extension: //rest-console-id
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Settings.py

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.authentication.TokenAuthentication',
        'rest_framework.permissions.IsAdminUser',


    ),
    'PAGINATE_BY': 10
}
Run Code Online (Sandbox Code Playgroud)

view.py

class ProfileList(generics.ListCreateAPIView):
    """
    API endpoint that represents a list of users.
    """
    permission_classes = (permissions.IsAuthenticated,)
    model = Profile
    serializer_class = ProfileSerializer

    def pre_save(self, obj):
        obj.owner = self.request.user
Run Code Online (Sandbox Code Playgroud)

Fiv*_*ver 113

以防其他人遇到此错误.如果您使用mod_wsgi在Apache上运行Django,也会发生这种情况,因为mod_wsgi会剥离授权标头.您需要将以下内容添加到VirtualHost配置中:

WSGIPassAuthorization On


Tom*_*tie 27

假设您正在尝试使用TokenAuthentication,标头应如下所示:

Authorization: Token 6d82549b48a8b079f618ee9c51a6dfb59c7e2196
Run Code Online (Sandbox Code Playgroud)

文档中所述.

  • 你在REST_FRAMEWORK设置中设置了"TokenAuthentication"吗?见这里:http://django-rest-framework.org/api-guide/authentication.html#setting-the-authentication-scheme (2认同)
  • @ Falcon1你想分享你的解决方案吗?:) (2认同)

oma*_*rc7 19

我的令牌认证遇到了同样的问题

这解决了我的问题

settings.py

REST_FRAMEWORK = {
   'DEFAULT_AUTHENTICATION_CLASSES': (
       'rest_framework.authentication.TokenAuthentication',
   ),
   'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAdminUser'
   ),
   'PAGINATE_BY': 10,
}
Run Code Online (Sandbox Code Playgroud)

  • 添加`DEFAULT_AUTHENTICATION_CLASSES``rest_framework.authentication.TokenAuthentication`键对我有用. (4认同)

Sli*_*eam 7

在我的情况下,这工作:
(Django REST Framework v3)

settings.py

REST_FRAMEWORK = {
   'DEFAULT_AUTHENTICATION_CLASSES': (
       'rest_framework.authentication.TokenAuthentication',
       'rest_framework.authentication.SessionAuthentication',
   ),
   'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
   ),
}
Run Code Online (Sandbox Code Playgroud)

views.py

class Test(APIView):
    def get(self, request, format=None):   
        return Response({'Result': 'OK'})
Run Code Online (Sandbox Code Playgroud)

urls.py

router.add_api_view('test', url(r'^test/', views.Test.as_view(),name='test'))
Run Code Online (Sandbox Code Playgroud)

不要忘记在标题中发送令牌信息:

 Key: Authorization  
 Value: Token 76efd80cd6849ad7d35e04f1cc1eea35bdc20294
Run Code Online (Sandbox Code Playgroud)

要生成令牌,您可以使用以下内容(代码中的某个位置):

from rest_framework.authtoken.models import Token            
user = User.objects.get(username='<username>')
token = Token.objects.create(user=user)
print(token.key)
Run Code Online (Sandbox Code Playgroud)

  • 这个答案需要得到更多的支持。关键思想是将 `rest_framework.authentication.TokenAuthentication` 放在 `DEFAULT_AUTHENTICATION_CLASSES` 中,而不是放在 DRF v3.xx 中的 `DEFAULT_PERMISSION_CLASSES` (2认同)