4

OAuth2.0 を django-rest-framework に統合しました。認証されたリクエストをクラスベースのビューに送信すると、これが得られました

{
    "detail": "You do not have permission to perform this action."
}

設定.py

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

ビュー.py

    from rest_framework import permissions
    from oauth2_provider.contrib.rest_framework import TokenHasReadWriteScope
    class LogoutView(APIView):
        """
            This will help in logout of user.
        """
        authentication_classes = ()
        permission_classes = (permissions.IsAuthenticated, TokenHasReadWriteScope)

        def get(self, request):
            return Response({'s': 'd'})

urls.py

from django.urls import path, re_path
from accounts.views import SignUpView, LoginView, LogoutView

urlpatterns = [
    path('signup/', SignUpView.as_view()),
    path('login/', LoginView.as_view()),
    path('logout/', LogoutView.as_view()),
]

そして、これは私のヘッダーがどのように見えるかです

Content-Type:application/json
Authorization:Bearer 4A7qGgmHpbEWlJn5w4wCwxJ9jWfTZ5

これが私が生成したアクセストークンです。

4

1 に答える 1