Django Tastypie:如何使用 API 密钥进行身份验证
我正在使用 TastyPie 制作一个内部 API。我
from tastypie.authentication import ApiKeyAuthentication
class MyResource(ModelResource):
Meta:
authentication = ApiKeyAuthentication()
禁用了身份验证规则,我的 API 工作得很好。启用它后,无论我尝试什么,我都会收到 401(未经授权)响应。
我相信,一旦您看到它的实际效果,这是非常明显的事情之一,但与此同时,请建议如何发出请求(GET)。
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 username 和 api_key 参数添加到 GET 变量中。确保您
在设置时遵循文档中的其他说明:
ApiKeyAuthentication
作为要求密码等敏感数据的替代方案,ApiKeyAuthentication 允许您仅收集用户名和密码。机器生成的 api 密钥。 Tastypie 附带了一个专门用于此目的的特殊模型,因此您需要确保 tastypie 位于 INSTALLED_APPS 中。
Tastypie 包含一个信号函数,可用于自动创建 ApiKey 对象。连接起来看起来像:
Add the username and api_key parameters to your GET variables. Make sure that you have the
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: