當前位置: 首頁>>代碼示例>>Python>>正文


Python decorators.oauth_required方法代碼示例

本文整理匯總了Python中oauth2client.django_util.decorators.oauth_required方法的典型用法代碼示例。如果您正苦於以下問題:Python decorators.oauth_required方法的具體用法?Python decorators.oauth_required怎麽用?Python decorators.oauth_required使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在oauth2client.django_util.decorators的用法示例。


在下文中一共展示了decorators.oauth_required方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: oauth_required

# 需要導入模塊: from oauth2client.django_util import decorators [as 別名]
# 或者: from oauth2client.django_util.decorators import oauth_required [as 別名]
def oauth_required(decorated_function=None, scopes=None, **decorator_kwargs):
    """ Decorator to require OAuth2 credentials for a view


    .. code-block:: python
       :caption: views.py
       :name: views_required_2


       from oauth2client.django_util.decorators import oauth_required

       @oauth_required
       def requires_default_scopes(request):
          email = request.credentials.id_token['email']
          service = build(serviceName='calendar', version='v3',
                       http=request.oauth.http,
                       developerKey=API_KEY)
          events = service.events().list(
                                    calendarId='primary').execute()['items']
          return HttpResponse("email: %s , calendar: %s" % (email, str(events)))

    :param decorated_function: View function to decorate, must have the Django
           request object as the first argument
    :param scopes: Scopes to require, will default
    :param decorator_kwargs: Can include ``return_url`` to specify the URL to
           return to after OAuth2 authorization is complete
    :return: An OAuth2 Authorize view if credentials are not found or if the
             credentials are missing the required scopes. Otherwise,
             the decorated view.
    """

    def curry_wrapper(wrapped_function):
        @wraps(wrapped_function)
        def required_wrapper(request, *args, **kwargs):
            return_url = decorator_kwargs.pop('return_url',
                                              request.get_full_path())
            user_oauth = django_util.UserOAuth2(request, scopes, return_url)
            if not user_oauth.has_credentials():
                return shortcuts.redirect(user_oauth.get_authorize_redirect())
            setattr(request, django_util.oauth2_settings.request_prefix,
                    user_oauth)
            return wrapped_function(request, *args, **kwargs)

        return required_wrapper

    if decorated_function:
        return curry_wrapper(decorated_function)
    else:
        return curry_wrapper 
開發者ID:0x0ece,項目名稱:oscars2016,代碼行數:51,代碼來源:decorators.py

示例2: oauth_required

# 需要導入模塊: from oauth2client.django_util import decorators [as 別名]
# 或者: from oauth2client.django_util.decorators import oauth_required [as 別名]
def oauth_required(decorated_function=None, scopes=None, **decorator_kwargs):
    """ Decorator to require OAuth2 credentials for a view.


    .. code-block:: python
       :caption: views.py
       :name: views_required_2


       from oauth2client.django_util.decorators import oauth_required

       @oauth_required
       def requires_default_scopes(request):
          email = request.credentials.id_token['email']
          service = build(serviceName='calendar', version='v3',
                       http=request.oauth.http,
                       developerKey=API_KEY)
          events = service.events().list(
                                    calendarId='primary').execute()['items']
          return HttpResponse(
              "email: {0}, calendar: {1}".format(email, str(events)))

    Args:
        decorated_function: View function to decorate, must have the Django
           request object as the first argument.
        scopes: Scopes to require, will default.
        decorator_kwargs: Can include ``return_url`` to specify the URL to
           return to after OAuth2 authorization is complete.

    Returns:
        An OAuth2 Authorize view if credentials are not found or if the
        credentials are missing the required scopes. Otherwise,
        the decorated view.
    """
    def curry_wrapper(wrapped_function):
        @wraps(wrapped_function)
        def required_wrapper(request, *args, **kwargs):
            if not (django_util.oauth2_settings.storage_model is None or
                    request.user.is_authenticated()):
                redirect_str = '{0}?next={1}'.format(
                    django.conf.settings.LOGIN_URL,
                    parse.quote(request.path))
                return shortcuts.redirect(redirect_str)

            return_url = decorator_kwargs.pop('return_url',
                                              request.get_full_path())
            user_oauth = django_util.UserOAuth2(request, scopes, return_url)
            if not user_oauth.has_credentials():
                return shortcuts.redirect(user_oauth.get_authorize_redirect())
            setattr(request, django_util.oauth2_settings.request_prefix,
                    user_oauth)
            return wrapped_function(request, *args, **kwargs)

        return required_wrapper

    if decorated_function:
        return curry_wrapper(decorated_function)
    else:
        return curry_wrapper 
開發者ID:taers232c,項目名稱:GAMADV-XTD,代碼行數:61,代碼來源:decorators.py

示例3: oauth_required

# 需要導入模塊: from oauth2client.django_util import decorators [as 別名]
# 或者: from oauth2client.django_util.decorators import oauth_required [as 別名]
def oauth_required(decorated_function=None, scopes=None, **decorator_kwargs):
    """ Decorator to require OAuth2 credentials for a view


    .. code-block:: python
       :caption: views.py
       :name: views_required_2


       from oauth2client.django_util.decorators import oauth_required

       @oauth_required
       def requires_default_scopes(request):
          email = request.credentials.id_token['email']
          service = build(serviceName='calendar', version='v3',
                       http=request.oauth.http,
                       developerKey=API_KEY)
          events = service.events().list(
                                    calendarId='primary').execute()['items']
          return HttpResponse(
              "email: {0}, calendar: {1}".format(email, str(events)))

    :param decorated_function: View function to decorate, must have the Django
           request object as the first argument
    :param scopes: Scopes to require, will default
    :param decorator_kwargs: Can include ``return_url`` to specify the URL to
           return to after OAuth2 authorization is complete
    :return: An OAuth2 Authorize view if credentials are not found or if the
             credentials are missing the required scopes. Otherwise,
             the decorated view.
    """

    def curry_wrapper(wrapped_function):
        @wraps(wrapped_function)
        def required_wrapper(request, *args, **kwargs):
            return_url = decorator_kwargs.pop('return_url',
                                              request.get_full_path())
            user_oauth = django_util.UserOAuth2(request, scopes, return_url)
            if not user_oauth.has_credentials():
                return shortcuts.redirect(user_oauth.get_authorize_redirect())
            setattr(request, django_util.oauth2_settings.request_prefix,
                    user_oauth)
            return wrapped_function(request, *args, **kwargs)

        return required_wrapper

    if decorated_function:
        return curry_wrapper(decorated_function)
    else:
        return curry_wrapper 
開發者ID:Servir-Mekong,項目名稱:ecodash,代碼行數:52,代碼來源:decorators.py


注:本文中的oauth2client.django_util.decorators.oauth_required方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。