python - Django Tastypie : How to Authenticate with API Key

标签 python django tastypie

我正在使用 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/

相关文章:

python - 在 Django Tastypie API 中包含子资源

python - 检查具有特定条件的连续数字 - Python

python - 加载数据本地 infile 在 Ubuntu 12.04 和 MySQL 中不起作用

python - 在单个图中,所有列的箱线图被 "label"列分割

python - 使用 Tastypie 公开模型方法

python - Django Tastypie 使用 PATCH 或 PUT 请求进行多对多( self )字段更新?

python - 如何检查 NaN 值?

Django:M2M数据库注解查询

python请求登录网站返回403

python - 如何在 django 的查询集中使用 If else