Post 数据到 table 在概念与 Apps 脚本
Post data to table in Notion with Apps Script
我正在测试概念 API,我想将数据从 Apps 脚本推送到我的概念 table。
这是我到目前为止得到的代码和下面的错误。我假设我的 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`."}
找到了,这里是文档的解决方案:
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);
}
我正在测试概念 API,我想将数据从 Apps 脚本推送到我的概念 table。 这是我到目前为止得到的代码和下面的错误。我假设我的 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`."}
找到了,这里是文档的解决方案:
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);
}