如何使用 ApiKeyAuthentication 在 Django TastyPie 中发出 post 请求

How to make post request in Django TastyPie using ApiKeyAuthentication

我有这样的资源:

class EntryResource(ModelResource):
    class Meta:
        queryset = Entry.objects.all()
        resource_name = 'entry'
        allowed_methods = ['post']
        authentication = ApiKeyAuthentication()
        authorization = Authorization()

并尝试根据documentation请求该资源:

requests.post('http://localhost/api/entry/',
              json={"key1": "value1",
                    "key2": "value2"},
              headers={"content-type": "application/json",
                       "Authorization": "ApiKey",
                       "<username>": "<api_key>"})

但是得到401

来自文档:

Authorization: ApiKey daniel:204db7bcfafb2deb7506b89eb3b9b715b09905c8

您的要求必须是这样的:

requests.post('http://localhost/api/entry/',
              json={"key1": "value1",
                    "key2": "value2"},
              headers={"content-type": "application/json",
                       "Authorization": "ApiKey <username>:<api_key>"})