使用Notion API获取父页面中内联子数据库的ID

问题描述 投票:0回答:1

我正在尝试获取 Notion 页面上内联子数据库的数据库 ID。

我尝试使用 GET 方法检索父页面。 (如下面的代码所示)

import requests, json

# Define my Notion API API_KEY
API_KEY = "secret_MyKey_:)"

# Define the Notion page URL
get_url = "https://api.notion.com/v1/pages/parent_page_id_:)"

# Set up headers for the API request
headers = {
    "Authorization": f"Bearer {API_KEY}",
    "Content-Type": "application/json",
    "Notion-Version": "2022-06-28",
}

# Making the API request
response = requests.get(page_url, headers=headers)

# Saving the results to a JSON file if the request is successful
if response.status_code == 200:
    data = response.json()
    with open("test.json", "w") as f:
        json.dump(data, f)
else:
    print(f"Failed to fetch child elements. Status code: {response.status_code}")

生成的 JSON 文件如下所示。

{
    "object": "page",
    "id": "page_id",
    "created_time": "2023-09-14T02:02:00.000Z",
    "last_edited_time": "2023-09-14T10:53:00.000Z",
    "created_by": {
        "object": "user",
        "id": "my_id_:)"
    },
    "last_edited_by": {
        "object": "user",
        "id": "me :)"
    },
    "cover": null,
    "icon": { "type": "emoji", "emoji": "\ud83d\udd2c" },
    "archived": false,
    "properties": {
        "title": {
            "id": "title",
            "type": "title",
            "title": [
                {
                    "type": "text",
                    "text": { "content": "Testing Zone", "link": null },
                    "annotations": {
                        "bold": false,
                        "italic": false,
                        "strikethrough": false,
                        "underline": false,
                        "code": false,
                        "color": "default"
                    },
                    "plain_text": "Testing Zone",
                    "href": null
                }
            ]
        }
    },
    "url": "https://www.notion.so/Testing-Zone-page_id",
    "public_url": null
}

显然,子元素没有键。我想知道是否有另一种方法可以专门访问页面的子ID。如果你能帮助我,我会很高兴! 😇

python get notion-api
1个回答
0
投票

https://api.notion.com/v1/pages/<page_id>)
- 仅返回页面的属性,而不返回页面内容。更多相关内容这里

https://api.notion.com/v1/blocks/<page_id>/children
- 使用它来检索页面的内容,该页面也将具有内联数据库 ID。更多相关内容这里

数据库的一页,也是一个块。因此,我们使用 block-children API 来检索页面(块)的子项。

© www.soinside.com 2019 - 2024. All rights reserved.