Django Tastypie: How to Authenticate with API Key

后端 未结 1 1233
轻奢々
轻奢々 2020-12-03 04:04

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

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


        
相关标签:
1条回答
  • 2020-12-03 04:19

    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)
    
    0 讨论(0)
提交回复
热议问题