[英]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.