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

Jas*_*ein 14 python django tastypie

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

from tastypie.authentication import ApiKeyAuthentication
class MyResource(ModelResource):
  Meta:
    authentication = ApiKeyAuthentication()
Run Code Online (Sandbox Code Playgroud)

禁用Auth规则后,我的API运行良好.有了它,无论我尝试什么,我都会收到401(未经授权)的回复.

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

Iss*_*lly 19

将用户名和api_key参数添加到GET变量中.确保你有

curl http://localhost:8000/api/v1/books/?username=issackelly\&api_key=123456789adfljafal
Run Code Online (Sandbox Code Playgroud)

设置时,请务必遵循teh docs中的其他说明:

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)
Run Code Online (Sandbox Code Playgroud)

  • 在将请求发送到服务器时,我在哪里获得APIKey? (3认同)