社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Django

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

HuLu ViCa • 4 年前 • 856 次点击  

我想用 邮递员 测试我的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
 
856 次点击  
文章 [ 2 ]  |  最新文章 4 年前