在使用python创建字段时,无法验证Airtable API

时间:2018-06-16 07:37:43

标签: python python-3.x curl airtable

[新手问题]

我正在尝试使用python 3在我的Airtable基础上创建一条新记录。 文档中的curl命令如下:

import requests

API_URL = "https://api.airtable.com/v0/restoftheurl"

data = {"Authorization": "Bearer My_API_Key","Content-type": 
"application/json","fields": {"Item": "randomitem","Quantity": 
"5","Customer_ID": ["randomrecord"]}}

r = requests.post(API_URL, data)
print(r.json())

我尝试使用的python代码是:

{'error': {'type': 'AUTHENTICATION_REQUIRED', 'message': 'Authentication required'}}

响应是错误的地方:

var exchangesViewActivated = false {
    didSet {
        if exchangesViewActivated == true {
            setExchangesView()
        } else {
            setUpLayout()
        }
    }
}

我应该如何正确地验证这一点,还是我的方式?

1 个答案:

答案 0 :(得分:0)

您需要区分正文(数据)和标题。使用json命名参数自动将内容类型设置为application/json

import requests

API_URL = "https://api.airtable.com/v0/restoftheurl"

headers = {
    "Authorization": "Bearer My_API_Key"
}

data = {
    "fields": {
        "Item": "randomitem",
        "Quantity": "5",
        "Customer_ID": ["randomrecord"]
    }
}

r = requests.post(API_URL, headers=headers, json=data)
print(r.json())