Django Tastypie:如何使用API​​密钥进行身份验证

时间:2011-10-18 21:42:33

标签: python django tastypie

我正在使用TastyPie制作内部API。我有

from tastypie.authentication import ApiKeyAuthentication
class MyResource(ModelResource):
  Meta:
    authentication = ApiKeyAuthentication()

禁用Auth规则后,我的API效果很好。有了它,无论我尝试什么,我都会获得401(未经授权)的响应。

我确信这是一旦你看到它在行动中非常明显的事情之一,但在此期间,请告知如何提出请求(GET)。

1 个答案:

答案 0 :(得分:19)

将用户名和api_key参数添加到GET变量中。

确保您拥有

curl http://localhost:8000/api/v1/books/?username=issackelly\&api_key=123456789adfljafal

确保在设置时遵循其他文档中的其他说明:

ApiKeyAuthentication

作为要求敏感数据(如密码)的替代方案,ApiKeyAuthentication允许您只收集用户名和密码。机器生成的api密钥。 Tastypie为此提供了一个特殊型号,因此您需要确保tastypie处于INSTALLED_APPS状态。

Tastypie包含一个信号函数,可用于自动创建ApiKey对象。挂起它看起来像:

from django.contrib.auth.models import User
from django.db import models
from tastypie.models import create_api_key

models.signals.post_save.connect(create_api_key, sender=User)