Harshit Chauhan
Harshit Chauhan

Reputation: 155

django rest framework TokenAuthentication not working

Want to use token base authentication system so api call for get list using DRF , It always throw error , I test this api in local system.

"detail":"Authentication credentials were not provided."

Setting.py

REST_FRAMEWORK = {

    'DEFAULT_AUTHENTICATION_CLASSES': (
        #'rest_framework.permissions.IsAuthenticated',

        'rest_framework.authentication.BasicAuthentication',
        'rest_framework.authentication.SessionAuthentication',
        'oauth2_provider.ext.rest_framework.OAuth2Authentication',        
        'rest_framework.authentication.TokenAuthentication',        
    ),

    'DEFAULT_RENDERER_CLASSES': (
        'rest_framework.renderers.JSONRenderer',
        'rest_framework.renderers.BrowsableAPIRenderer',
    ),

    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
        'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly',

    ),       
}

Serializer.py

class MyListSerializer(SignUpSerializer):

    class Meta:
        model = MyMod
        fields = ('no', 'yes')       

view.py

class MyList(generics.ListCreateAPIView):

    queryset = MyMod.objects.all()
    serializer_class = MyListSerializer
    authentication_classes = (TokenAuthentication,)

url:

curl -H "Authorization: Bearer MDgYnKeoRsp0O4Hfgr9ka5tdfkKs6Y" http://127.0.0.1:8000/my/

Error:

{"detail":"Authentication credentials were not provided."}

Upvotes: 0

Views: 3098

Answers (1)

Harshit Chauhan
Harshit Chauhan

Reputation: 155

Problem :

class MyList(generics.ListCreateAPIView):

    queryset = MyMod.objects.all()
    serializer_class = MyListSerializer
    authentication_classes = (TokenAuthentication,)

Solution:

class MyList(generics.ListCreateAPIView):

    queryset = MyMod.objects.all()
    serializer_class = MyListSerializer
    permission_classes = [TokenHasReadWriteScope]

curl -H "Authorization: Bearer MDgYnKeoRsp0O4Hfgr9ka5tdfkKs6Y" http://127.0.0.1:8000

Upvotes: 1

Related Questions