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

Sebastien

My question is related to this one and this one but for some significant differences: for the first reference: I use django-oauth-toolkit although unlike the second reference, the user MUST be authenticated as this is not a registering endpoint but an upload one. I have successfully implemented other endpoints within the same application with the same setup and it works appropriately.

For example:

class projectsView(mixins.ListModelMixin,
                  mixins.CreateModelMixin,
                  generics.GenericAPIView):
    queryset = Project.objects.all()
    serializer_class = ProjectSerializer

    def get(self, request, *args, **kwargs):
        return self.list(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        return self.create(request, *args, **kwargs)

    def perform_create(self, serializer):
        serializer.save(owner=self.request.user)

and it's model and serializer and urls works as expected. However this one:

 class uploadView(mixins.ListModelMixin,
                  mixins.CreateModelMixin,
                  generics.GenericAPIView):

     queryset = FileUpload.objects.all()
     parser_classes = (MultiPartParser, FormParser,) #(FileUploadParser,)
     serializer_class = FileUploadSerializer

     def post(self, request, *args, **kwargs):
         print(request.data['file'])
         return self.create(request, *args, **kwargs)

     def perform_create(self, serializer):
         serializer.save(owner=self.request.user, project_id=self.kwargs['pk'],
                      file=self.request.data['file'])

Does not as it returns {"detail":"Authentication credentials were not provided."} with code 401.

There is the minor detail that the "pk" parameter from the url references explicitly the corresponding project id as from it's url instruction: path('projects/<uuid:pk>/upload/', views.uploadView.as_view(), name='upload'),. But apart from that, as far as I can tell, the only difference is the parser_classes.

I use curl to test locally on my machine if this works and here is the curl instruction:

curl \
        -vvv \
        -X POST \
        --form "file=@$FILE_NAME" \
        --header "Authorization: Token $(cat token)" \
        "$URL"

Where $FILE_NAME is an excel file in this case and $URL is set to http://localhost:8000/<prefix>/projects/<project id>/upload/. The project id is valid as tested with the $URL value of http://localhost:8000/<prefix>/projects/<project id>/ with GET instead of POST and no --form option.

Why does the Bearer token from the oauth2 scheme works in the first example but not in the second? Is it related to the parsers or something else? And how to fix it?

Sebastien

The error is in the curl instruction: The default django-oauth-toolkit token keyword is not "Token" but "Bearer".

curl \
    -vvv \
    -X POST \
    --form "file=@$FILE_NAME" \
    --header "Authorization: Bearer $(cat token)" \
    "$URL"

works.

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 - Authentication credentials were not provided

Python Requests with Django Rest Framework - 'detail': 'Authentication credentials were not provided'

Authentication credentials were not provided with Django Rest Framework JWT

Django Authentication credentials were not provided

How to customize [Authentication credentials were not provided] error message in Django rest framework

How to fix {detail: Authentication credentials were not provided.} in Django Rest Framework

How to solve "detail": "Authentication credentials were not provided." error for Class-based APIView Django REST Framework?

Authentication credentials were not provided django-rest-auth

Django : "detail": "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

Forbidden: /api/v1.0/user/create-user/ & { "detail": "Authentication credentials were not provided." } DJANGO

Extra protection layer for Django Rest Framework and OAuth2 Toolkit

Django OAuth Toolkit: could not import ext.rest_framework

Authentication credentials were not provided drf

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

"Authentication credentials were not provided." in DRF

Authentication credentials were not provided with djangorestframework-jwt

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

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

Using Django REST Framework as an authentication backend for Django

Django Rest Framework says No input provided?

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