Django REST Framework ViewSet Authentication Token issue
I have a small API in my Django project, I've been following tutorials for authentication tokens. I'm able to generate and store tokens, but whenever I do a cURL request to the API, with the token, it always returns unauthenticated, even though the token is correct. The endpoint worked before I applied the token authentication. Any help would be great, code is below:
api/views.py
from rest_framework import viewsets
from .serializers import PlatformSerializer
from rest_framework.authentication import TokenAuthentication
from rest_framework.permissions import IsAuthenticated
from userplatforms.models import Platform
class PlatformViewSet(viewsets.ModelViewSet):
authentication_classes = [TokenAuthentication]
permission_classes = [IsAuthenticated]
serializer_class = PlatformSerializer
queryset = Platform.objects.all().order_by('user_id')
api/urls.py
from django.urls import path, include
from rest_framework import routers
from rest_framework.authtoken import views as auth_views
from . import views
router = routers.DefaultRouter()
router.register(r'userplatforms', views.PlatformViewSet)
urlpatterns = [
path('', include(router.urls)),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
path('authtoken/', auth_views.obtain_auth_token, name='tokenauth')
]
api/serializers.py
from rest_framework import serializers
from userplatforms.models import Platform
class PlatformSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Platform
fields = ('id', 'user_id', 'platform_reference', 'platform_variables')
settings.py
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.TokenAuthentication',
)
}
INSTALLED_APPS = [
'api.apps.ApiConfig',
'userplatforms.apps.UserplatformsConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_extensions',
'rest_framework',
'rest_framework.authtoken'
]