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

需要一个使用django tastypie进行授权的示例吗,django,tastypie,Django,Tastypie,我对Django及其生态系统比较陌生。我正在使用django tastypie为我们的移动客户端编写RESTAPI。我已经浏览了web上几乎所有关于如何使用tastypie创建REST接口的示例。但它们都不是特定于从客户发布数据以及如何授权客户的 如示例所示,我使用了来自tastypie.authentication.BasicAuthentication的。它会打开一个弹出窗口,询问用户名和密码,并在浏览器上正常工作。但我不确定,它是否会在手机上做同样的事情(具体来说,是本机IOS应用程序)。

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

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


我对这件事完全不知所措,我非常感谢你的帮助

您可以签出源代码并使用例如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)
谢谢你的帮助

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

我编写了一个类来处理身份验证和重写is_authenticated方法。然后我可以在tastypie资源类的元定义中使用这个类

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() 从tastype.authentication导入基本身份验证 从tastypie.resources导入资源,模型资源 #用于处理身份验证的类 类别MyAuthentication(基本身份验证): def已通过身份验证(自我、请求、**kwargs): #将检查请求对象的用户名和密码的逻辑放在这里 #如果用户经过身份验证,则返回True,否则返回False #tastypie资源类 类MyResource(模型资源): 类元: 身份验证=MyAuthentication()
这将确保访问资源的请求将通过您的身份验证代码。

这实际上不是django和Tastype特有的。您应该检查您正在进行POST请求的库是否支持在请求时填写用户名/密码。问题是身份验证,未授权。请在下面的注释中附加这些示例代码。请在此处输入逻辑,以检查请求对象的用户名和密码。如果用户已通过身份验证,则返回True,否则返回False@timus2001当然,这里有一个可能的解决方案。