我对Django和它的生态系统比较陌生。我正在使用django-tastypie为我们的移动客户端编写REST api。我已经在网上浏览了几乎所有关于如何使用tastypie来创建REST接口的示例。但它们都不是特定于POSTing来自客户端的数据,以及如何授权客户端。
我在示例中使用了from tastypie.authentication.BasicAuthentication。它会打开一个弹出窗口,询问用户名和密码,在浏览器上运行良好。但我不确定,它是否会在移动设备上做同样的事情(具体地说,是原生IOS应用程序)。我不太明白什么时候用户会请求登录,如果他或她使用的不是浏览器,而是本地应用程序,他/她的移动设备上将如何显示此弹出窗口。
我对此完全不知所措,真的很感谢你的帮助。
发布于 2012-01-13 23:34:26
您可以签出源代码并使用例如ApiKeyAuthentication。您只需将用户名和api密钥发布到认证用户即可。
它看起来可用于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
https://stackoverflow.com/questions/7253171
复制相似问题