0

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."}
5
  • 2
    Aren't you mixing OAuth2Authentication and TokenAuthentication ? for TokenAuthentication, you should use this header : Authorization: Token MDgYnKeoRsp0O4Hfgr9ka5tdfkKs6Y ("Token", not "Bearer") Commented Mar 30, 2015 at 8:24
  • thanks for reply , according to you : curl -i -H "Authorization: Token MDgYnKeoRsp0O4Hfgr9ka5tdfkKs6Y" 127.0.0.1:8000/my same error {"detail":"Authentication credentials were not provided."} Commented Mar 30, 2015 at 9:23
  • @RaphaëlBraud is right way to use TokenAuthentication in class view. Commented Mar 30, 2015 at 9:29
  • Prefixing Authorization header value with 'Token ' is the right way but how did you get "MDgYnKeoRsp0O4Hfgr9ka5tdfkKs6Y" ? you should use something like : Token.objects.get_or_create(user=user).key . Is it the case ? Commented Mar 30, 2015 at 12:55
  • main problem with permission permission_classes = [TokenHasReadWriteScope] instead of authentication_classes = (TokenAuthentication,) Commented Apr 3, 2015 at 12:28

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.