【问题标题】:Post method via Airtable API using a Google Script使用 Google 脚本通过 Airtable API 发布方法
【发布时间】:2022-02-07 00:57:04
【问题描述】:

我在 Google 脚本中有以下代码:

var data = {
    "records": [
          {
          "fields": {
              "Contract Address": "test",
              "0x8df3aad3a84da6b69a4da8aec3ea40d9091b2ac4": "1234"
          }
          }
    ]
};

var options = {
  "method" : "post",
  'Content-Type': 'application/json',
  'muteHttpExceptions' : true,
  // "payload" : data,
  "payload" : JSON.stringify(data)
};

function tryAPost(){
  var url = "https://api.airtable.com/v0/xxxxxxxxxxxxx/Balance%20Tracking?api_key=keyxxxxxxxxxx";
  var response = UrlFetchApp.fetch(url, options);
  //console.log(response.getContentText());
 console.log(response.getResponseCode());
};

我收到以下回复:

422

而且数据最终不会出现在 Airtable 中。

payload 在 Postman 中的 post 请求正文中工作。

我做错了什么?

根据评论编辑:

这是 airtable 中的示例代码:

var Airtable = require('airtable');
var base = new Airtable({apiKey: 'YOUR_API_KEY'}).base('xxxxxxxxxxxx');

base('Balance Tracking').create([
  {
    "fields": {
      "Contract Address": "Thu, 03 Feb 2022 15:12:37 GMT",
      "0xfecf784f48125ccb7d8855cdda7c5ed6b5024cb3": 12055358359168

为每条评论添加邮递员截图:

【问题讨论】:

  • 您需要向我们提供一些文档,因为 api 文档似乎并不公开。试试"payload" : JSON.stringify(data)
  • 我将 airtable 中的示例代码添加到问题中。我试过JSON.strigify(data),但没用。
  • 这是 JSON.stringify(object)
  • 你至少需要显示邮递员截图。此外,您应该将响应记录为文本:console.log(response.getContentText())
  • @Cooper 很抱歉这么厚,但我认为data 是一个对象。

标签: api google-apps-script urlfetch airtable http-status-code-422


【解决方案1】:

我在 Airtable 论坛上提问,有人提出了一个可行的解决方案。

这是link。

答案如下:

//
// post to Airtable (universal)
//
function atPostTable_(baseKey, tableName, payload)
{
  var options = 
      {
        method: 'POST',
        headers: {
          'Authorization' : 'Bearer ' + cMyAirtableAPIKey,
          'Content-Type'  : 'application/json'
        },
        payload : JSON.stringify(payload),
        muteHttpExceptions : true,
        followRedirects: true
      };
  var response = UrlFetchApp.fetch(cAirtableAPIEndpoint + baseKey + "/" + encodeURIComponent(tableName), options).getContentText();
  return(response);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-02
    • 2018-10-22
    • 1970-01-01
    • 2020-02-06
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多