You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Notion API向数据库页面插入Quote块时遇请求URL无效错误

问题原因与解决方案

出现「Invalid request URL」错误的核心原因是HTTP请求方法错误:向Notion页面添加子块需要使用PATCH方法,你的代码中误用了POST方法。

Notion API规则明确:

  • POST /v1/blocks:用于创建独立的无父级块
  • PATCH /v1/blocks/{block_id}/children:用于向指定块(页面属于顶级块)追加子块

修改后的代码

仅需修改创建块的请求方法,其余代码保持不变:

import requests
import json

# Notion API access token
token = "................."
database_id = "............."

# API endpoint to retrieve pages in the database
get_pages_url = f'https://api.notion.com/v1/databases/{database_id}/query'

# API request to retrieve pages in the database
headers = {
    "Authorization": "Bearer " + token,
    "Content-Type": "application/json",
    "Notion-Version": "2022-06-28",
}

response = requests.post(get_pages_url, headers=headers)

if response.status_code == 200:
    data = response.json()

    # Select the first page in the database
    if len(data['results']) > 0:
        page_id = data['results'][0]['id']

        # API endpoint to append block children
        create_block_url = f'https://api.notion.com/v1/blocks/{page_id}/children'

        # JSON payload for the new quote block
        block_payload = {
            'children': [
                {
                    'object': 'block',
                    'type': 'quote',
                    'quote': {
                        'text': [
                            {
                                'type': 'text',
                                'text': {
                                    'content': 'This is a quote.'
                                }
                            }
                        ]
                    }
                }
            ]
        }

        # 修改请求方法为PATCH
        response = requests.patch(create_block_url, headers=headers, data=json.dumps(block_payload))

        # Check the response
        if response.status_code == 200:
            print('New quote block successfully added to the page in the database.')
        else:
            print('Error creating the block:', response.status_code, response.text)
    else:
        print('The database contains no pages.')
else:
    print('Error retrieving pages from the database:', response.status_code, response.text)

额外验证项

  1. 确认你的Notion Token已开启pages:update权限(在集成设置中配置)
  2. 页面ID无需去除连字符,带格式的ID可直接用于API请求

内容的提问来源于stack exchange,提问作者Tobi.M

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 05:35:20