1
votes

I am using the Django Restful API Framework together with Simple JWT and have successfully created a URL for receiving and refreshing a user token. In order to try out the authentication using the token, I have created a view that simply lists all the posts inside the database. I have then assigned the IsAuthenticated class to the view.

As expected, I get an error message saying that the authentication credentials were not provided. I then went ahead and made a simple GET request using Postman, with the authentication token provided in the "Authorization" tab. The type was set to "Bear Token". Unfortunately, I still get the message "Authentication credentials were not provided." with a 403 Forbidden code.

I have also tried to provide the token in the Headers, as well as make CURL requests, everything to no avail.

My view looks like this:

class PostListView(generics.ListAPIView):
    permission_classes = (IsAuthenticated,)
    queryset = Post.objects.filter()

This is the serializer:

class PostListSerializer(serializers.ModelSerializer):
    class Meta:
        model = Post
        fields = ('__all__')

The settings.py of the Django project:

REST_FRAMEWORK = {
    'DEFAULT_FILTER_BACKENDS': ['django_filters.rest_framework.DjangoFilterBackend'],
    'DEFAULT_PERMISSION_CLASSES': ['rest_framework.permissions.AllowAny'],
    'DEFAULT_AUTHENTICATION_CLASSES:': ('rest_framework_simplejwt.authentication.JWTAuthentication',)
}
CORS_ALLOW_ALL_ORIGINS = True  # For testing purposes

I have followed several different tutorials online, read through numerous posts as well as followed the official documentation of Simple JWT.

1

1 Answers

0
votes

Well what you are doing is trying to filter the data while your basic purpose is to just list your model. For filtering make sure your go through the documentation DRF Filtering.

Try these changes in your code. I hope it will work for you.

Settings.py

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework_simplejwt.authentication.JWTAuthentication',
    ],
}

Views.py

class UserList(generics.ListAPIView):
    permission_classes = (IsAuthenticated,)
    queryset = Post.objects.all()
    serializer_class = PostListSerializer

After this try to hit your API with access token. To learn more about generic views you can go through this link Generic Views in DRF.