English 中文(简体)
需要有使用django-tastypie的授权实例
原标题:need an example of doing authorization using django-tastypie

我是Django的相对新鲜事物,具有生态系统。 我是用django-tastypie为我们的流动客户撰写的REST一皮书。 我已经走到了网络上几乎所有关于如何利用信使建立教育与技术接口的例子。 但是,其中没有一个具体涉及从客户收集数据,以及你如何授权客户。

我使用的是信使。 实例显示的基本假设。 它向用户名和密码开放,并在浏览器上进行罚款。 但是,我不相信,如果在移动(具体来说,是本土的IOS仪器)上做同样的事情的话。 如果用户不使用浏览器,而是使用本地的传承器,那么他/她的移动装置将如何显示这种登峰。

我完全没有这样做,我真的会赞赏你的帮助。

最佳回答

得到帮助。

我使用了@Iurii提到的类似做法。 这就是我的解决办法。

我写了一个处理认证的班子,压倒性是认证方法。 之后,我可以将这一类别用于塔斯蒂皮类资源类别的梅塔定义。


    from tastypie.authentication import BasicAuthentication
    from tastypie.resources import Resource, ModelResource

    # class for handling authentication
    class MyAuthentication(BasicAuthentication):
        def is_authenticated(self, request, **kwargs):
            # put here the logic to check username and password from request object
            # if the user is authenticated then return True otherwise return False

    # tastypie resource class
    class MyResource(ModelResource):
        class Meta:
            authentication = MyAuthentication()

这将确保通过您的认证法申请获得资源。

问题回答

You can check out source and use for example ApiKeyAuthentication. You just have to POST username and api key to authentificate user.

It looks like usable for ios app. Here is the part of the checking code.

def is_authenticated(self, request, **kwargs):
    """
    Finds the user and checks their API key.

    Should return either ``True`` if allowed, ``False`` if not or an
    ``HttpResponse`` if you need something custom.
    """
    from django.contrib.auth.models import User

    username = request.GET.get( username ) or request.POST.get( username )
    api_key = request.GET.get( api_key ) or request.POST.get( api_key )

    if not username or not api_key:
        return self._unauthorized()

    try:
        user = User.objects.get(username=username)
    except (User.DoesNotExist, User.MultipleObjectsReturned):
        return self._unauthorized()

    request.user = user
    return self.get_key(user, api_key)

https://github.com/toastdriven/django-tastypie/blob/master/tastypie/authentication.py#L128 https://github.com/toastdriven/django-tastypie/blob/master/tastypie/authorization.py#L42





相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签