【问题标题】:Trouble authenticating Airtable API while creating a field with python使用 python 创建字段时无法验证 Airtable API
【发布时间】:2018-06-16 07:37:43
【问题描述】:

[新手问题]

我正在尝试使用 python 3 在我的 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'}}

我应该如何正确验证这一点,或者我可以这样做?

【问题讨论】:

    标签: python python-3.x curl airtable


    【解决方案1】:

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

    【讨论】:

    • 谢谢,这已经解决了。
    猜你喜欢
    • 1970-01-01
    • 2014-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多