Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python/tastypie:对非资源的授权?_Python_Api_Tastypie - Fatal编程技术网

python/tastypie:对非资源的授权?

python/tastypie:对非资源的授权?,python,api,tastypie,Python,Api,Tastypie,使用tastypi作为我们这里的API 因此,主要的API类是: from tastypie.api import Api class TheAPI(Api): def prepend_urls(self): return [ url(r"^(?P<api_name>%s)/reset/(?P<pk>\w+)%s$" % (self.api_name, trailing_slash()), self.wrap_view('reset'), name

使用tastypi作为我们这里的API

因此,主要的API类是:

from tastypie.api import Api

class TheAPI(Api):
  def prepend_urls(self):
    return [
     url(r"^(?P<api_name>%s)/reset/(?P<pk>\w+)%s$" % (self.api_name, trailing_slash()), self.wrap_view('reset'), name="api_reset"),
    ]
  def reset(self, request, **kwargs):
      #do the work
到目前为止还不错。 我的问题在于我想在这个调用中使用API身份验证。我不希望任何人能够使用复位功能,以及有一个独特的代码在网址,但仍然

但由于这不是一种资源,我不知道如何做到这一点。我尝试向这个类添加一个元类,但它似乎被忽略了

我唯一能想到的另一种黑客是发明一些封装此功能的伪造资源,但这感觉很奇怪,因为它不是一种资源。
有什么想法吗?

在这里扩展常规资源并将授权和身份验证添加到元类中没有坏处

class BaseNonORMResource(Resource):

    class Meta:
        max_limit = None

        """
        (Using Tastypie's DjangoAuthorization which checks the permission a user has granted to them)
        and Tastypie's token based authentication approach
        """
        authorization = DjangoAuthorization()
        authentication = ApiKeyAuthentication()

        '''
        Specify allowed_methods, list_allowed_methods or detail_allowed_methods in each SubResource
        e.g. allowed_methods = ['get']
        '''
        allowed_methods = ['get', 'post', 'delete', 'patch', 'put']
然后使用它来定义端点

class TheAPI(BaseNonORMResource):
  def prepend_urls(self):
    return [
         url(r"^(?P<api_name>%s)/reset/(?P<pk>\w+)%s$" % (self.api_name, trailing_slash()), self.wrap_view('reset'), name="api_reset"),
    ]

  def reset(self, request, **kwargs):
      #do the work

嘿,不确定那个密码是否正确。prepend_URL应该返回一个列表,并且在代码中没有返回任何内容。