![](/img/trans.png)
[英]In django-rest-framework, is it possible to use oauth and session authentication simultaneously?
[英]Django-rest-framework with django OAuth 2.0 giving authentication error
我已經將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()),
]
注冊用戶不需要任何身份驗證。 因此,您需要這樣寫您的視圖。
class SignUpView(APIView):
"""
Signup for the user.
"""
authentication_classes = ()
permission_classes = ()
def get(self, request):
return Response({'result': True, 'message': 'User registered successfully.'})
對於所有其他請求,您需要在標頭中傳遞身份驗證令牌。 在這種情況下,您將無需提及身份驗證和權限類,因為將使用默認類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.