首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我需要一个使用django-tastypie进行授权的示例

我需要一个使用django-tastypie进行授权的示例
EN

Stack Overflow用户
提问于 2011-08-31 14:06:39
回答 1查看 4.2K关注 0票数 8

我对Django和它的生态系统比较陌生。我正在使用django-tastypie为我们的移动客户端编写REST api。我已经在网上浏览了几乎所有关于如何使用tastypie来创建REST接口的示例。但它们都不是特定于POSTing来自客户端的数据,以及如何授权客户端。

我在示例中使用了from tastypie.authentication.BasicAuthentication。它会打开一个弹出窗口,询问用户名和密码,在浏览器上运行良好。但我不确定,它是否会在移动设备上做同样的事情(具体地说,是原生IOS应用程序)。我不太明白什么时候用户会请求登录,如果他或她使用的不是浏览器,而是本地应用程序,他/她的移动设备上将如何显示此弹出窗口。

我对此完全不知所措,真的很感谢你的帮助。

EN

回答 1

Stack Overflow用户

发布于 2012-01-13 23:34:26

您可以签出源代码并使用例如ApiKeyAuthentication。您只需将用户名和api密钥发布到认证用户即可。

它看起来可用于ios应用程序。这是检查代码的一部分。

代码语言:javascript
复制
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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7253171

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档