0

我正在测试概念 API,我想从 Apps Script 将数据推送到我的 Notion 表中。这是我到目前为止得到的代码和下面的错误。我假设我的 json 结构不正确,但我不知道如何修复它

function tryout_notion () {

  const url = "https://api.notion.com/v1/pages";
  //tried with : https://api.notion.com/v1/databases

  var options = {
                  'muteHttpExceptions': true,
                  "method" : "post",
                  "headers": {
                              Authorization: `Bearer secret_*****`,
                              "Content-Type": "application/json",
                              "Notion-Version": "2021-05-13",
                            },
                  "parent": {
                    "page_id": "*****"
                    //tried with : "database_id"
                  },
                  "properties": {
                    "Name": {
                      "title": [
                        {
                          "text": {
                            "content": "CREATE NEW LINE IN NOTION"
                          }
                        }
                      ]
                    }
                  }
                };
  const response = UrlFetchApp.fetch(url, options);
  Logger.log(response);
}
{"object":"error","status":400,"code":"validation_error","message":"body failed validation: body.parent should be defined, instead was `undefined`."}
4

1 回答 1

0

找到它,这里是文档的解决方案:

function tryout_notion () {

const url = "https://api.notion.com/v1/pages";

var payload = {
                "parent": {
                  "type": "database_id",
                  "database_id": "*****"
                },
                "properties": {
                  "Name": {
                    "title": [
                      {
                        "text": {
                          "content": "CREATE NEW LINE IN NOTION"
                        }
                      }
                    ]
                  }
                }

  }

  var options = {
                  'muteHttpExceptions': true,
                  "method" : "post",
                  "headers": {
                              Authorization: `Bearer secret_*****`,
                              "Content-Type": "application/json",
                              "Notion-Version": "2021-05-13",
                            },
                  "payload": JSON.stringify(payload)

                };
  const response = UrlFetchApp.fetch(url, options);
  Logger.log(response);
}
于 2021-08-05T14:35:38.817 回答