【问题标题】:How to use tastypie for showing contents of model based on client_id in oauth 2.0?在 oauth 2.0 中,如何使用 sweetpie 显示基于 client_id 的模型内容?
【发布时间】:2014-08-03 06:33:21
【问题描述】:

我有一个问题,我想根据字段过滤模型资源并将特定查询集显示给请求的 client_id。

我正在使用带有 django 1.6.5 和 swagger UI 的tastepie v0.10.0 文档。

在示例模型中,我存储了与所有客户端相关的信息,并希望根据客户端 ID 显示数据属于特定客户端。我在示例模型中有过滤器字段,基于这些字段我可以为特定客户端创建查询集。

class Resource(ModelResource):
    class Meta:
        queryset = Example.objects.all()
        resource_name = 'example'
        authorization = DjangoAuthorization()
        detail_allowed_methods = ['get',]
        authentication = OAuth20Authentication()

请建议我实施上述方案的最佳方法。提前致谢。

【问题讨论】:

    标签: python django rest tastypie


    【解决方案1】:

    我想到了两种可能性,如何根据 client_id 过滤资源。我想 client_id 是您模型的一个字段:

    或者您可以使用美味派过滤:

    class Resource(ModelResource):
        class Meta:
            queryset = Example.objects.all()
            resource_name = 'example'
            authorization = DjangoAuthorization()
            detail_allowed_methods = ['get',]
            authentication = OAuth20Authentication()
            filtering = {
                'client_id': 'exact',
            }
    

    这一次,你应该通过 GET 方法传递过滤参数,例如:

    http://website.com/api/?client_id=5
    

    或者你可以编写自己的 obj_get_list 方法::

    class Resource(ModelResource):
        class Meta:
            queryset = Example.objects.all()
            resource_name = 'example'
            authorization = DjangoAuthorization()
            detail_allowed_methods = ['get',]
            authentication = OAuth20Authentication()
    
        def obj_get_list(self, bundle, **kwargs):
            queryset = Message.objects.filter(client_id = bundle.request.GET['client_id'])
            return queryset
    

    【讨论】:

    • 感谢您的回复,但我的模型中没有 client_id。 provider.oauth2 就是这样。我只需在 HTTP 请求的标头中获取 AccessToken,就可以获取客户端 ID。你能告诉我从哪里可以得到 Header Info 吗?
    • 我认为您可以通过 bundle.request.META['AccesToken?'] 访问 HTTP 标头
    • 我通过 bundle.request.META.get("HTTP_AUTHORIZATION") 获得了 Header 但 obj_get_list 不起作用。它给了我存储在模型中的全部数据。
    • 但对于我提出的问题,它并不完全正确。使用 oauth2。
    【解决方案2】:

    这是我提出的解决方案。

    import provider.oauth2
    from provider.oauth2.models import AccessToken
    
    
    class Resource(ModelResource):
        # 
        class Meta:
            queryset = Example.objects.all()
            resource_name = 'example'
            authorization = DjangoAuthorization()
            detail_allowed_methods = ['get',]
            always_return_data = True
            authentication = OAuth20Authentication()
    
            key = bundle.request.GET.get('oauth_consumer_key')
            if not key:
                key = bundle.request.POST.get('oauth_consumer_key')
            if not key:
                auth_header_value = bundle.request.META.get('HTTP_AUTHORIZATION')
                if auth_header_value:
                   key = auth_header_value.split(' ')[1]
    
            token = verify_access_token(key)
            >>>>> token.user this contains username
            >>>> come out with the proper conditions through which you can create queryset
            queryset = Example.objects.filter(field=condition)
            return queryset
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-01
      • 1970-01-01
      • 2016-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多