【问题标题】:Django-rest-framework with django OAuth 2.0 giving authentication error带有 django OAuth 2.0 的 Django-rest-framework 给出身份验证错误
【发布时间】:2018-07-24 03:57:27
【问题描述】:

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

这是我的 settings.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()),

]

【问题讨论】:

    标签: python django oauth django-rest-framework django-oauth


    【解决方案1】:

    注册用户不需要任何身份验证。所以你需要这样写你的视图。

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

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

    【讨论】:

      猜你喜欢
      • 2017-01-25
      • 2019-01-01
      • 1970-01-01
      • 2016-03-27
      • 2020-08-21
      • 2018-03-25
      • 2021-09-09
      • 2013-05-03
      • 2018-12-11
      相关资源
      最近更新 更多