本文介绍了带有Django OAuth 2.0的Django-rest-framework给出了身份验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将django-rest-framework与django-oauth-toolkit集成在一起.它为我{"detail": "Authentication credentials were not provided."}提供了未经身份验证的api.

I have integrated django-rest-framework with django-oauth-toolkit. And it is giving me {"detail": "Authentication credentials were not provided."} with un authenticated apis.

这是我的设置.py

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'oauth2_provider.contrib.rest_framework.OAuth2Authentication',
    ),
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
    )
}

views.py

from rest_framework.views import APIView
from rest_framework.response import Response


class SignUpView(APIView):
    """
        Signup for the user.
    """
    def get(self, request):
        return Response({'result': True, 'message': 'User registered successfully.'})

urls.py

from django.urls import path
from myapp.views import SignUpView

urlpatterns = [
    path('signup/', SignUpView.as_view()),

]

推荐答案

注册用户不需要任何身份验证.因此,您需要这样写您的视图.

For registering a user, you do not need any authentication. So you need to write your view like this.

class SignUpView(APIView):
    """
        Signup for the user.
    """
    authentication_classes = ()
    permission_classes = ()

    def get(self, request):
        return Response({'result': True, 'message': 'User registered successfully.'})

对于所有其他请求,您需要在标头中传递身份验证令牌.在这种情况下,您将无需提及身份验证和权限类,因为将使用默认类.

For all other requests, you need to pass auth token in your header. In that case, you will not have any need to mention authentication and permission classes as your default classes will be used.

这篇关于带有Django OAuth 2.0的Django-rest-framework给出了身份验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 08:17