基于函数的视图

Django OAuth工具包提供了装饰器来帮助您保护基于函数的视图。

protected_resource(scopes=None, validator_cls=OAuth2Validator, server_cls=Server)

修饰器通过提供开箱即用的OAuth2身份验证来保护视图,可以选择使用作用域处理。基本用法,不使用作用域:

from oauth2_provider.decorators import protected_resource

@protected_resource()
def my_view(request):
    # An access token is required to get here...
    # ...
    pass

如果您希望在访问视图时也检查作用域,可以将它们作为修饰符的参数::

from oauth2_provider.decorators import protected_resource

@protected_resource(scopes=['can_make_it can_break_it'])
def my_view(request):
    # An access token AND the right scopes are required to get here...
    # ...
    pass

如果您希望或需要使用您自己的OAuth2逻辑::

from oauth2_provider.decorators import protected_resource
from myapp.oauth2_validators import MyValidator

@protected_resource(validator_cls=MyValidator)
def my_view(request):
    # You have to leverage your own logic to get here...
    # ...
    pass
rw_protected_resource(scopes=None, validator_cls=OAuth2Validator, server_cls=Server)

修饰器通过提供OAuth2身份验证和开箱即用的读/写作用域来保护视图。 GETHEADOPTIONS HTTP方法需要 'read' 作用域。否则 'write' 范围是必需的::

from oauth2_provider.decorators import rw_protected_resource

@rw_protected_resource()
def my_view(request):
    # If this is a POST, you have to provide 'write' scope to get here...
    # ...
    pass

如果你需要,你可以要求其他的范围。 'read''write' **

from oauth2_provider.decorators import rw_protected_resource

@rw_protected_resource(scopes=['exotic_scope'])
def my_view(request):
    # If this is a POST, you have to provide 'exotic_scope write' scopes to get here...
    # ...
    pass