Django Rest Framework - Authentication credentials were not provided

前端 未结 12 1691
野性不改
野性不改 2020-12-04 07:12

I\'m developing an API using Django Rest Framework. I\'m trying to list or create an \"Order\" object, but when i\'m trying to access the console gives me this error:

<
12条回答
  •  臣服心动
    2020-12-04 08:11

    I too faced the same since I missed adding

    authentication_classes = (TokenAuthentication)

    in my API view class.

    class ServiceList(generics.ListCreateAPIView):
        authentication_classes = (SessionAuthentication, BasicAuthentication, TokenAuthentication)
        queryset = Service.objects.all()
        serializer_class = ServiceSerializer
        permission_classes = (IsAdminOrReadOnly,)
    

    In addition to the above, we need to explicitly tell Django about the Authentication in settings.py file.

    REST_FRAMEWORK = {
       'DEFAULT_AUTHENTICATION_CLASSES': (
       'rest_framework.authentication.TokenAuthentication',
       )
    }
    

提交回复
热议问题