Py学习  »  Django

带有postman的django rest框架令牌身份验证

HuLu ViCa • 4 年前 • 837 次点击  

我想用 邮递员 测试我的drf终点,但我总是得到一个 Authentication credentials were not provided. 错误。端点工作正常,但我还没有找到如何从 邮递员 .

我可以为用户获取令牌:

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'
}
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/43666
 
837 次点击  
文章 [ 2 ]  |  最新文章 4 年前
waqasgard
Reply   •   1 楼
waqasgard    5 年前

你可以尝试改变 Token Bearer 在请求正文中。

所以,看起来应该是:

Bearer <token>

Sergey Pugach
Reply   •   2 楼
Sergey Pugach    5 年前

在使用apache时,您需要:

WSGIPassAuthorization On

添加到您的 httpd.conf

更多细节 there .