基于类的视图
Django OAuth工具包提供了泛型类,可用于使用 Class Based View 接近。
- ProtectedResourceView(ProtectedResourceMixin, View):
提供开箱即用的OAuth2身份验证的视图。要实施受保护的端点,只需将您的CBV定义为::
class MyEndpoint(ProtectedResourceView): """ A GET endpoint that needs OAuth2 authentication """ def get(self, request, *args, **kwargs): return HttpResponse('Hello, World!')
Please notice :
OPTION方法不受OAuth2保护,无法允许印前检查请求。
- ScopedProtectedResourceView(ScopedResourceMixin, ProtectedResourceView):
提供开箱即用的OAuth2身份验证和作用域处理的视图。要实现受保护的端点,只需定义CBV并指定
required_scopes领域::class MyScopedEndpoint(ScopedProtectedResourceView): required_scopes = ['can_make_it can_break_it'] """ A GET endpoint that needs OAuth2 authentication and a set of scopes: 'can_make_it' and 'can_break_it' """ def get(self, request, *args, **kwargs): return HttpResponse('Hello, World!')
- ReadWriteScopedResourceView(ReadWriteScopedResourceMixin, ProtectedResourceView):
提供OAuth2身份验证和读/写默认作用域的视图。
GET,HEAD,OPTIONSHTTP方法需要read作用域,其他方法需要write作用域。如果需要,您始终可以在required_scopes领域::class MyRWEndpoint(ReadWriteScopedResourceView): required_scopes = ['has_additional_powers'] # optional """ A GET endpoint that needs OAuth2 authentication and the 'read' scope. If required_scopes was specified, clients also need those scopes. """ def get(self, request, *args, **kwargs): return HttpResponse('Hello, World!')
DOT中的通用视图由一组Mixin组成,您可以在 views.mixins 模块:如果您想提供自己的基于类的视图,可以直接使用这些混合。