使用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)
额外验证项
- 确认你的Notion Token已开启
pages:update权限(在集成设置中配置) - 页面ID无需去除连字符,带格式的ID可直接用于API请求
内容的提问来源于stack exchange,提问作者Tobi.M
相关产品推荐
相关产品推荐

