【问题标题】:How to make post request in Django TastyPie using ApiKeyAuthentication如何使用 ApiKeyAuthentication 在 Django TastyPie 中发出发布请求
【发布时间】:2016-10-12 13:44:35
【问题描述】:

我有这样的资源:

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。

【问题讨论】:

    标签: python django tastypie


    【解决方案1】:

    来自文档:

    授权:A​​piKey daniel:204db7bcfafb2deb7506b89eb3b9b715b09905c8

    您的请求必须是这样的:

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

    【讨论】:

    • 酷!这很容易。
    猜你喜欢
    • 2014-03-17
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-08
    • 2020-05-03
    • 2018-05-04
    • 1970-01-01
    • 2015-10-25
    相关资源
    最近更新 更多