Django Tastypie:如何使用API​​密钥进行身份验证 [英] Django Tastypie: How to Authenticate with API Key

查看:241
本文介绍了Django Tastypie:如何使用API​​密钥进行身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

禁用Auth规则后,我的API工作得很好。有了它,我会得到一个401(UNAUTHORIZED)的回应,无论我尝试什么。



我确信这是其中一个很明显,一旦你有看到它在行动,但在此期间,请指导如何提出请求(一个GET)。

解决方案

添加用户名和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来自django的用户
导入模型
从tastypie.models导入create_api_key

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


I'm making an internal API with TastyPie. I have

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

With Auth rules disabled, my API works great. With it on, I get a 401 (UNAUTHORIZED) response no matter what I try.

I'm sure this is one of those things that's really obvious once you've see it in action, but in the meantime, please advise how to to make the request (a GET).

解决方案

Add the username and api_key parameters to your GET variables. Make sure that you have the

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

Make sure to follow the other instructions from teh docs when setting it up:

ApiKeyAuthentication

As an alternative to requiring sensitive data like a password, the ApiKeyAuthentication allows you to collect just username & a machine-generated api key. Tastypie ships with a special Model just for this purpose, so you'll need to ensure tastypie is in INSTALLED_APPS.

Tastypie includes a signal function you can use to auto-create ApiKey objects. Hooking it up looks like:

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)

这篇关于Django Tastypie:如何使用API​​密钥进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆