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

标签 django tastypie

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

我使用了 from tastepie.authentication.BasicAuthentication ,如示例中所示。它会打开一个弹出窗口,询问用户名和密码,并在浏览器上正常工作。但我不确定它是否会在移动设备上做同样的事情(具体来说,原生 IOS 应用程序)。当用户请求登录时,如果他或她不使用浏览器而是使用 native 应用程序,我不太明白这个弹出窗口将如何显示在他/她的移动设备上。

我完全迷失了这一点,我非常感谢你的帮助。

最佳答案

您可以查看源代码并使用例如 ApiKeyAuthentication。
您只需发布用户名和 api key 即可验证用户。

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

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

关于django - 需要一个使用 django-tastypie 进行授权的示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7253171/

相关文章:

django - 在 Django 中合并两个模型实例

Django buggy 模板标签 - 'NoneType' 对象没有属性 'source'

python - django - 部署到 Heroku : Server Error(500)

python - Django Rest Framework - Django 嵌套序列化器一对多关系

ios - 将 tastypie API 限制在我的应用程序中

javascript - 将非 AMD 兼容的 javascript 模块与 require.js 一起使用?

python - 在 tastypie-swagger 中指定参数

python - Django 子 url 不显示其模板

python - Tastypie:不要发送资源的整个 uri(只是 id)

python - Tastypie 与 application/x-www-form-urlencoded