- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 settings.py
文件中,我编写了以下设置:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'oauth2_provider.contrib.rest_framework.OAuth2Authentication',
),
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
}
当我使用来自密码授予
应用程序的 token 调用任何API时,它工作正常,但是当我尝试使用来自客户端凭据授予的 token 调用相同的API时
应用程序,它不起作用,并且响应 403 错误:
{ "detail": "You do not have permission to perform this action." }.
是因为默认权限吗?我不知道我必须使用什么权限!?
最佳答案
终于解决了!问题是我使用的权限
。事实上,当您使用客户端凭据
授予时,IsAuthenticated
权限会检查request.user
,该权限为None
。由于 DRF 中没有支持客户端凭据
授予的权限
,因此您必须使用自己的 DRF custom permission 。这是我需要和使用的:
from rest_framework.permissions import BasePermission
class IsAuthenticatedOrClientCredentialPermission(BasePermission):
def has_permission(self, request, view):
if request.auth is None:
return False
grant_type = request.auth.application.get_authorization_grant_type_display()
if request.user is None:
if grant_type == 'Client credentials':
request.user = request.auth.application.user # <-- this is because I needed to get the user either the grant is 'password' or 'client credentials'
return True
else:
return False
else:
return True
但是您可能只想拥有一个权限来检查授予类型是否为客户端凭据
并授予权限,如果是这样,这就是您所需要的:
from rest_framework.permissions import BasePermission
class ClientCredentialPermission(BasePermission):
def has_permission(self, request, view):
if request.auth is None:
return False
grant_type = request.auth.application.get_authorization_grant_type_display()
if request.user is None and grant_type == 'Client credentials':
return True
else:
return False
注意:如果您想使用第二个自定义权限,请注意request.user
为None
,您可以获取客户端的所有者(正在向您发送请求)通过 request.auth.application.user
。
您可以通过将自定义权限添加到适当的 View 来使用它们。 (就像使用 rest_framework.permissions
下的任何 DRF 权限一样)
基于类别的 View :
class ExampleView(APIView):
permission_classes = [ClientCredentialPermission] # <-- Add your permissions to this list
def get(self, request, format=None):
content = {
'status': 'request was permitted'
}
return Response(content)
基于功能的 View :
@api_view(['GET'])
@permission_classes([ClientCredentialPermission]) # <-- Add your permissions to this list
def example_view(request, format=None):
content = {
'status': 'request was permitted'
}
return Response(content)
关于python - 当我尝试以客户端身份请求时,DRF 通过 403 错误响应我 [客户端凭据授予],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58010166/
背景 我有一个定义了多个自定义操作的 ModelViewSet。我在 urls.py 中使用默认路由器注册 URL。现在,我的 View 使用默认创建的路由,如 ^images/$ , ^images
DRF 中的 request.data 和 DRF 中的 serializers.data 有什么区别。 当我在 DRF 中编写基于函数的 View 时,我会像使用它们一样 - eli
我用Django REST框架准备了API。我正在使用docker运行我的API,一切在我的机器上都运行得很好。因此,我只需运行docker-compose up,就可以在浏览器中使用Swagger测
我用Django REST框架准备了API。我正在使用docker运行我的API,一切在我的机器上都运行得很好。因此,我只需运行docker-compose up,就可以在浏览器中使用Swagger测
快速上手JWT签发token和认证,有这一篇就够了,DRF自带的和自定义的都帮你总结好了,拿去用~ DRF JWT认证(二) 上篇中对JWT有了基本的认知,这篇来略谈JWT的使用 签发:一般我们登录成
什么接口文档都不用手写了?自动挡? DRF 自动生成接口文档 我们知道前后端分离,前端一般按后端写好的接口去开发,那么就需要我们明细后端接口数据等,需要写接口文档,前端按照接口文档去开发 接口文档如何
为什么要使用JWT认证?构成和原理又是什么?怎么还有Base64的事?我都写了👆 DRF JWT认证(一) JWT认证 Json web token (JWT), 是为了在网络应用环境间传递声明而执
DRF 中如何使用过滤,排序,分页,以及报错了如何处理?10分钟get了~ DRF 过滤排序分页异常处理 过滤 涉及到查询数据的接口才需要过滤功能 DRF过滤使用种类: 内置过滤类 第三方 自定义 内
前后端分离开发怎么给接口做认证权限频率三大认证?点进来5分钟get! DRF-认证权限频率 前后端混合开发,可以通过HttpResponse对象来设置cookie进而校验登录,现在前后端分离开发,用不
你问我使用DRF视图组件快速写接口?九九归一剑诀早就准备好了,没有副作用! DRF 视图组件 DRF框架提供了很多通用的视图基类与扩展类,上篇使用的APIView是比较偏Base的,视图的使用更加简化
1、路由Routers 在 Rest Framework 中提供了两个 router , 可以帮助我们快速的实现路由的自动生成。 必须是继承 ModelViewSet 的视图类才能自动生成路由
1. 前言 大家好,我是安果! 接下来,将通过几篇文章将对 DRF 模型序列化进行展开说明,详情谈谈模型序列化的作用、步骤及进阶用法 2. 模型序列化的作用?  
我第一次尝试进入 Django 原子事务。我想知道是否有可能像这样使用它: class TaskViewSet(MultiSerializerViewSet): @transaction.at
我有一个 drf 序列化程序,其中有一个我想重命名的字段: class UserBulkUploadSerializer(serializers.Serializer): ... is
我使用了 drf 并且还使用了 serializer 来检查用户输入。 这是我的serializer.py class BoardSerializer(serializers.Serializer):
我正在尝试序列化一个 CreateUserSerializer(ModelSerializer)我的代码如下。我的问题是它只会创建一个 User 而不是 UserProfile。 模型.py clas
我如何发布这个 JSON { "campaign": 27, "campaignName": "Prueba promo", "promotionType": 999, "items"
我正在构建一个 todo 应用程序,可以在 todo 上发布评论。 为此,我制作了一个可写的双嵌套序列化程序。它有效,但我编写的更新方法难以理解,因此我试图使其更具可读性。是否有更好的(或标准的)方法
有没有办法为特定的 View 类指定所需的自定义 header ?在我正在使用的 API 上,一些信息是使用 header 传递的,如果缺少 header ,我需要返回 BAD_REQUEST 或类似
我正在使用 DRF 并且我正在尝试创建一个对象,该对象具有多个外键以及需要在此过程中创建的相关对象。 这是我的模型的简化版本: class Race(models.Model): name =
我是一名优秀的程序员,十分优秀!