使用Postman

时间:2018-12-18 08:44:35

标签: python django django-rest-framework postman

我正在尝试使用 Postman 来测试我的DRF端点,但是我总是遇到Authentication credentials were not provided.错误。端点工作正常,但是我还没有找到如何从 Postman 发送请求。

我能够为用户获取令牌

enter image description here

但是当我尝试使用令牌发送请求时,总是会出现错误:

enter image description here

我将授权类型设置为Inherit auth from parent

这是该视图的代码:

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)

这是我REST_FRAMEWORK中的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 个答案:

答案 0 :(得分:1)

就使用Apache而言,您需要:

WSGIPassAuthorization On

将其添加到您的httpd.conf

更多详细信息there

答案 1 :(得分:0)

您可以尝试在请求正文中将Token更改为Bearer

因此,它看起来应该像:

Bearer <token>

答案 2 :(得分:0)

添加标题:

Cookie sessionid={token}

enter image description here