Search code examples
djangodjango-rest-frameworkdjango-rest-auth

DRF: Unable to make authenticated calls with token in header


I have a list view that is authenticated but whenever I try to make a request with the token in the header, I get a 400 error. Any ideas?

list view

class EventListView(ListAPIView):
    authentication_classes = (TokenAuthentication, )
    permission_classes = (IsAuthenticated, )
    serializer_class = EventFilterSerializer
    ...

get auth token endpoint:

from rest_framework.authtoken.views import obtain_auth_token

urlpatterns = [
    path('auth/', obtain_auth_token, name='api_token_auth'),
]

settings:


REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework.authentication.TokenAuthentication',
    ],
}

Postman Screenshot

Postman Screenshot


Solution

  • As stated by @Iain Shelvington, even though I had DEFAULT_AUTHENTICATION_CLASSES permissions configured in settings, I overrode it in the view. By removing the empty authentication_classes tuple entirely or adding TokenAuthTokenAuthenticationen, my issue was resolved.