Django Rest Framework Token Authentication with Postman

Viewed 5545

I am trying to use Postman to test my DRF end-point, but I always get an Authentication credentials were not provided. error. The end-point works properly, but I haven't found how to send the request from Postman.

I am able to get the token for the user:

enter image description here

But I always get the error when I try to use the token to send a request:

enter image description here

I set the Authorization Type to Inherit auth from parent.

This is the code for the view:

class AlbumViewSet(viewsets.ModelViewSet):
    permission_classes = (permissions.IsAuthenticated,)
    queryset = proxies.AlbumProxy.objects.all()
    serializer_class = serializers.AlbumSerializer
    filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter,)
    search_fields = ('name', 'description', 'company__name')
    filter_fields = ('code', 'company')

    def get_permissions(self):
        if self.action == 'retrieve':
            return []

        return super(AlbumViewSet, self).get_permissions()

    def retrieve(self, request, pk):
        password = request.query_params.get('password', None)

        try:
            instance = proxies.AlbumProxy.objects.get(pk=pk)
        except:
            return Response({'success': False, 'code': 1})

        if instance.access_code != password and password != settings.MASTER_KEY:
            return Response({'success': False, 'code': 2})

        instance_to_return = serializers.AlbumSerializer(instance=instance, context={'request': request}).data
        instance_to_return.pop('access_code')
        instance_to_return['success'] = True

        return Response(instance_to_return)

This is my REST_FRAMEWORK constant from settings:

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAuthenticated',),
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.TokenAuthentication',
        'rest_framework.authentication.SessionAuthentication',
    ),
    'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',),
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
    'PAGE_SIZE': 20,
    'DEFAULT_METADATA_CLASS': 'rest_framework.metadata.SimpleMetadata'
}
3 Answers

As far as you use Apache you need to:

WSGIPassAuthorization On

add it to your httpd.conf

More details there.

You can try changing Token to Bearer in the request body.

So, it should look like:

Bearer <token>

Add in Headers:

Cookie sessionid={token}

enter image description here

Related