Мне удалось сделать первую часть (создание ожидаемого файла) < /p>
Скрипт для адаптации (документация по понятию): < /p>
Код: Выделить всё
// Open a read stream for the file
const fileStream = fs.createReadStream(filePath)
// Create form data with the (named) file contents under the `file` key.
const form = new FormData()
form.append('file', fileStream, {
filename: path.basename(filePath)
})
// HTTP POST to the Send File Upload API.
const response = await fetch(
`https://api.notion.com/v1/file_uploads/${fileUploadId}/send`,
{
method: 'POST',
body: form,
headers: {
'Authorization': `Bearer ${notionToken}`,
'Notion-Version': notionVersion,
}
}
)
// Rescue validation errors. Possible HTTP 400 cases include:
// - content length greater than the 20MB limit
// - FileUpload not in the `pending` status (e.g. `expired`)
// - invalid or unsupported file content type
if (!response.ok) {
const errorBody = await response.text()
console.log('Error response body:', errorBody)
throw new Error(`HTTP error with status: ${response.status}`)
}
const data = await response.json()
// ...
Вот одна из моих попыток, которые показывают эту ошибку: исключение: запрос не удастся для https://api.notion.com. Код 400
function uploadFileContentx(uploadUrl, file) {
var service = getService();
if (service.hasAccess()) {
const imgb64data = file.getBlob().getBytes();
var form = {
date : new Date(),
subject : "Picture joined",
comment : "xxxxxxxxxxxxx",
attachment1 : `data:image/png;base64,${imgb64data}`,
};
var options = {
method: 'POST',
muteHttpExceptions : false,
headers: {
'Notion-Version': API_VERSION,
Authorization: 'Bearer ' + service.getAccessToken(),
contentType: 'multipart/form-data'
},
body: {
'file' : `data:image/png;base64,${imgb64data}`
}
};
Logger.log(JSON.stringify(options, null, 2));
var response = UrlFetchApp.fetch(uploadUrl, options);
if (!response.ok) {
const errorBody = response.text
console.log('Error response body:', errorBody)
throw new Error(`HTTP error with status: ${response.status}`)
}
return response.getResponseCode() === 200;
} else {
var authorizationUrl = service.getAuthorizationUrl();
Logger.log('Open the following URL and re-run the script: %s',
authorizationUrl);
}
}
< /code>
Я бы очень признателен за любую помощь! Спасибо
Подробнее здесь: https://stackoverflow.com/questions/796 ... ipt-notion
Мобильная версия