最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Notion API 404 Error: Cannot Find Database when querying from Google Apps Script - Stack Overflow

programmeradmin2浏览0评论

I'm trying to query a Notion database using Google Apps Script, but I keep getting a 404 Not Found error.


My Setup

  • Using Google Apps Script (GAS) to query a Notion Database.
  • Notion API Version: 2022-06-28
  • Database ID: 1b950a040a82xxxxxxxxxxxxxxc72fc
  • Integration: gas_note_bot_v5 (with Full Access to the database)
  • Tested with Postman but still getting 404 Not Found.

My Code (Google Apps Script)

const NOTION_API_KEY = "ntn_xxxxxxxxxxxxxxxxxxxxxxx";
const DATABASE_ID = "1b950a040a82xxxxxxxxxxxxxxc72fc";

function testNotionAPI() {
    const url = `/${DATABASE_ID}/query`;
    const options = {
        method: "post",
        headers: {
            "Authorization": `Bearer ${NOTION_API_KEY}`,
            "Notion-Version": "2022-06-28",
            "Content-Type": "application/json"
        },
        muteHttpExceptions: true
    };
    const response = UrlFetchApp.fetch(url, options);
    Logger.log(response.getContentText());
}

Error Message

{
  "object": "error",
  "status": 404,
  "code": "object_not_found",
  "message": "Could not find database with ID: 1b950a040a82807b8209e36b588c72fc. Make sure the relevant pages and databases are shared with your integration."
}

What I've Tried

✅ Double-checked the Database ID from the Notion page URL
✅ Verified that the API Key is correct
✅ Ensured that Integration (gas_note_bot_v5) has Full Access to the database
✅ Tried querying using Postman, but still got 404 Not Found

I'm trying to query a Notion database using Google Apps Script, but I keep getting a 404 Not Found error.


My Setup

  • Using Google Apps Script (GAS) to query a Notion Database.
  • Notion API Version: 2022-06-28
  • Database ID: 1b950a040a82xxxxxxxxxxxxxxc72fc
  • Integration: gas_note_bot_v5 (with Full Access to the database)
  • Tested with Postman but still getting 404 Not Found.

My Code (Google Apps Script)

const NOTION_API_KEY = "ntn_xxxxxxxxxxxxxxxxxxxxxxx";
const DATABASE_ID = "1b950a040a82xxxxxxxxxxxxxxc72fc";

function testNotionAPI() {
    const url = `https://api.notion/v1/databases/${DATABASE_ID}/query`;
    const options = {
        method: "post",
        headers: {
            "Authorization": `Bearer ${NOTION_API_KEY}`,
            "Notion-Version": "2022-06-28",
            "Content-Type": "application/json"
        },
        muteHttpExceptions: true
    };
    const response = UrlFetchApp.fetch(url, options);
    Logger.log(response.getContentText());
}

Error Message

{
  "object": "error",
  "status": 404,
  "code": "object_not_found",
  "message": "Could not find database with ID: 1b950a040a82807b8209e36b588c72fc. Make sure the relevant pages and databases are shared with your integration."
}

What I've Tried

✅ Double-checked the Database ID from the Notion page URL
✅ Verified that the API Key is correct
✅ Ensured that Integration (gas_note_bot_v5) has Full Access to the database
✅ Tried querying using Postman, but still got 404 Not Found

Share Improve this question edited Mar 21 at 11:27 VLAZ 29.1k9 gold badges63 silver badges84 bronze badges asked Mar 20 at 16:31 ธเนศว์ ตั้งวัจนะโยบายธเนศว์ ตั้งวัจนะโยบาย 112 bronze badges 1
  • 1 Does this answer your question? stackoverflow/q/74818210 – TheMaster Commented Mar 21 at 0:26
Add a comment  | 

1 Answer 1

Reset to default 0

The question doesn't include enought details about the steps that should be followed to use the Notion API in Google Apps Script and might be interchanging concepts like workpsace, page and database. Below there a quick overvieow of the requirements, stops and a boilerplate code.

Requirements

  1. Have a Google account with access to Google Apps Script.
    Note: Google Workspace admin might block access to Google Apps Script.
  2. Have a Notion account.
  3. Be the owner of the Notion workspace

Steps

  1. Create a new integration in Notion. For details, see https://developers.notion/docs/create-a-notion-integration#create-your-integration-in-notion.
  2. Get your API secret. For details, see https://developers.notion/docs/create-a-notion-integration#create-your-integration-in-notion.
  3. Grab the required page ids and database ids.
    When using a free Notion plan and a personal workspace - a page URL ooks like this
    https://www.notion.so/Integration-Test-1be65a23f29f80a1af8dd1688afebc39.
    The page id is 1be65a23f29f80a1af8dd1688afebc39.
    - A database URL looks like this
    https://www.notion.so/1be65a23f29f80f18ab3f7f36509bf61?v=1be65a23f29f80a48dc2000ccf143d01.
    The database id is 1be65a23f29f80f18ab3f7f36509bf61.
  4. If you haven't done yet, create a new Apps Script project.
  5. Add the JavaScript code with the fetch or fetchAll methods from Class UrlFetchApp.

What about the following boilerplate?

const NOTION_API_KEY = "";
const NOTION_PAGE_ID = "";
const NOTION_DATABASE_ID = "";

function myFunction(){
    const url = "https://api.notion/v1/databases/"  + NOTION_DATABASE_ID;
    // const url = "https://api.notion/v1/pages/"  + NOTION_PAGE_ID;
    const options = {
        method: "get",
        // method: "post",
        // method: "patch",
        muteHttpExceptons: false,
        headers: {
            "Authorization": `Bearer ${NOTION_API_KEY}`,
            "Notion-Version": "2022-06-28",
            "Content-Type": "application/json"
        },
        muteHttpExceptions: true
    };
    const response = UrlFetchApp.fetch(url, options);
    Logger.log(JSON.stringify(response.getContentText(), null, " "));
}

Regarding querying a Notion's database, the code in the question missed the payload. See https://developers.notion/reference/post-database-query

发布评论

评论列表(0)

  1. 暂无评论