Django-rest-framework with django OAuth 2.0 giving authentication error

Nipun Garg

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.

Here's my 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()),

]
Muhammad Hassan

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Django rest_framework oAuth2 APIView error

Django REST Framework Swagger - Authentication Error

Authentication Against Company Django Rest OAuth2

Django Rest Framework Token Authentication

django rest framework no authentication no permission

Django REST framework no authentication details

Authentication failed django rest framework

JWT Authentication with Django REST Framework

Token Authentication with django rest framework

Django rest framework authentication issue

django-oauth-toolkit; Django Rest Framework - Authentication credentials were not provided

How to test an API endpoint with Django-rest-framework using Django-oauth-toolkit for authentication

Client Authentication in Django rest framework powered App using django-oauth-toolkit

Using Django REST Framework as an authentication backend for Django

Extra protection layer for Django Rest Framework and OAuth2 Toolkit

Disable or restrict /o/applications (django rest framework, oauth2)

Django Rest API with okta OAUTH token authentication

Intermittent failure: Django Rest Framework Authentication

Token authentication in django (rest_framework) not working

django rest framework - understanding authentication and logging in

Django Rest Framework Custom JWT authentication

Problem with JWT authentication in django-rest-framework

Authentication in Django rest framework function based views

Django Rest Framework Token Authentication with Postman

Django Rest Framework how to disable authentication and authorization

Token Authentication Not Working on Django Rest Framework

Django Rest Framework Postman Token Authentication

Django Rest Framework - Authentication credentials were not provided

Token Authentication Implementation in Django Rest Framework