0

I'm having an issue where I send a POST request to an api endpoint restaurant/api-token-auth and I get a 401 error with the json response:

{ "detail": "Invalid token." } 

This response is appearing even though I'm sending the right credentials in the POST request (username and password). In this case I tried my admin account and it still gives me this response.

This is my code for urls.py (app level):

from django.urls import path from . import views from rest_framework.authtoken.views import obtain_auth_token urlpatterns = [ path('home/', views.index, name='index'), path('menu/', views.MenuItemView.as_view()), path('menu/<int:pk>', views.SingleMenuItemView.as_view()), path('api-token-auth/', obtain_auth_token) ] 

urls.py (project level):

from django.contrib import admin from django.urls import path, include from rest_framework import routers from restaurant.views import BookingViewSet router = routers.DefaultRouter() router.register(r'tables', BookingViewSet) urlpatterns = [ path('admin/', admin.site.urls), path('restaurant/', include('restaurant.urls')), path('restaurant/booking/', include(router.urls)), path('auth/', include('djoser.urls')), path('auth/', include('djoser.urls.authtoken')), ] 

settings.py:

INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'restaurant', 'rest_framework', 'rest_framework.authtoken', 'djoser', ] ...(rest of code) REST_FRAMEWORK = { 'DEFAULT_RENDERER_CLASSES' : [ 'rest_framework.renderers.JSONRenderer', 'rest_framework.renderers.BrowsableAPIRenderer', ], 'DEFAULT_AUTHENTICATION_CLASSES' : [ 'rest_framework.authentication.TokenAuthentication', 'rest_framework.authentication.SessionAuthentication', ] } DJOSER = {"USER_ID_FIELD" : "username"} 

I tried manually creating tokens using the admin panel and using the djoser api endpoint auth/token/login and it works but this way it doesn't.

2
  • Something seems wrong here : path('auth/', include('djoser.urls')), path('auth/', include('djoser.urls.authtoken')), Commented May 12, 2024 at 9:55
  • Djoser works as far as I can see but DRF for some reason doesn't. Commented May 12, 2024 at 10:04

1 Answer 1

0

Found the solution, the code is right it's just that I was sending a bearer token with the credentials in the POST request instead of only sending the credentials in Insomnia.

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.