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

2024-04-29 10:41:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用tastype制作一个内部API。我有

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

禁用Auth规则后,我的API工作得很好。打开后,无论我尝试什么,都会得到401(未经授权)的响应。

我相信这是一件很明显的事情,一旦你看到它的行动,但同时,请建议如何提出请求(一个得到)。


Tags: fromimportauthapiauthentication规则tastypetastypie
1条回答
网友
1楼 · 发布于 2024-04-29 10:41:48

将用户名和api_键参数添加到GET变量中。确保你有

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

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

ApiKeyAuthentication

作为要求敏感数据(如密码)的替代方法,ApiKeyAuthentication允许您仅收集用户名和计算机生成的api密钥。Tastypie附带了一个专门用于此目的的模型,因此您需要确保Tastypie在已安装的应用程序中。

tastype包含一个信号函数,可用于自动创建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)

相关问题 更多 >