我正在使用 TastyPie 制作一个内部 API。我有
from tastypie.authentication import ApiKeyAuthentication
class MyResource(ModelResource):
Meta:
authentication = ApiKeyAuthentication()
在禁用身份验证规则的情况下,我的 API 运行良好。启用它后,无论我尝试什么,我都会收到 401(未经授权)响应。
我敢肯定,一旦您看到它的实际效果,这是非常明显的事情之一,但与此同时,请告知如何发出请求(GET)。
最佳答案
将用户名和 api_key 参数添加到您的 GET 变量中。确保您拥有
curl http://localhost:8000/api/v1/books/?username=issackelly\&api_key=123456789adfljafal
确保在设置时遵循文档中的其他说明:
ApiKeyAuthentication
作为要求敏感数据(如密码)的替代方法,ApiKeyAuthentication 允许您仅收集用户名和机器生成的 API key 。 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)
关于python - Django Tastypie : How to Authenticate with API Key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7814128/