Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python创建字段时验证Airtable API时出现问题_Python_Python 3.x_Curl_Airtable - Fatal编程技术网

使用python创建字段时验证Airtable API时出现问题

使用python创建字段时验证Airtable API时出现问题,python,python-3.x,curl,airtable,Python,Python 3.x,Curl,Airtable,[新手问题] 我正在尝试使用Python3在我的Airtable库中创建一个新记录。 文档中的curl命令如下所示: $ curl -v -XPOST https://api.airtable.com/v0/restoftheurl \ -H "Authorization: Bearer My_API_Key" \ -H "Content-type: application/json" \ -d '{ "fields": { "Item": "Headphone", "Qu

[新手问题]

我正在尝试使用Python3在我的Airtable库中创建一个新记录。 文档中的curl命令如下所示:

$ curl -v -XPOST https://api.airtable.com/v0/restoftheurl \
-H "Authorization: Bearer My_API_Key" \
-H "Content-type: application/json" \
 -d '{
  "fields": {
    "Item": "Headphone",
    "Quantity": "1",
    "Customer_ID": [
      "My_API_Key"
    ]
  }
}'
我尝试使用的python代码是:

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())
其中,响应为错误:

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

我应该如何正确地对此进行身份验证,或者我是如何进行身份验证的?

您需要区分正文(数据)和标题。使用
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())

谢谢,这个修好了。