Notion API:将引用块插入到数据库页面中

huangapple go评论62阅读模式
英文:

Notion API: Insert quote- block into a page of a database

问题

以下是您提供的代码的中文翻译:

import requests
import json

# Notion API 访问令牌
token = "................."
database_id = "............."

# 用于检索数据库中页面的 API 端点
get_pages_url = f'https://api.notion.com/v1/databases/{database_id}/query'

# 用于检索数据库中页面的 API 请求
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()

    # 选择数据库中的第一页
    if len(data['results']) > 0:
        page_id = data['results'][0]['id']

        # 用于创建新块的 API 端点
        create_block_url = f'https://api.notion.com/v1/blocks/{page_id}/children'

        # 新引用块的 JSON 载荷
        block_payload = {
            'children': [
                {
                    'object': 'block',
                    'type': 'quote',
                    'quote': {
                        'text': [
                            {
                                'type': 'text',
                                'text': {
                                    'content': '这是一段引用。'
                                }
                            }
                        ]
                    }
                }
            ]
        }

        # 创建块的 API 请求
        response = requests.post(create_block_url, headers=headers, data=json.dumps(block_payload))

        # 检查响应
        if response.status_code == 200:
            print('新引用块已成功添加到数据库中的页面。')
        else:
            print('创建块时出错:', response.status_code, response.text)
    else:
        print('数据库中没有页面。')
else:
    print('从数据库检索页面时出错:', response.status_code, response.text)

希望这有助于您理解代码和问题。如果您有任何其他问题,请随时提出。

英文:

With this code I try to insert a quote into an existing Notion page, but I get the following error: "Invalid request URL" which surprises me, because I have the url from the Notion website.
I actually just call the url and pass the new block: quote.
Fetching the Notion Page works without problems, only creating the block and adding it leads to an error, as this is output with "Error creating the block" from the print statement almost at the end

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 create a new block
        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.'
                                }
                            }
                        ]
                    }
                }
            ]
        }

        # API request to create the block
        response = requests.post(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

得分: 0

你正在使用POST方法进行API端点的操作,但我了解应该使用PATCH方法。你可以尝试使用PATCH方法,如果出现错误,请告诉我。

英文:

I have noticed that you are using POST method for that API endpoint. As I know it is a PATCH method. Can you try suing PATCH for that and if error keeps occurring let me know.

huangapple
  • 本文由 发表于 2023年5月21日 16:05:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76298875.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定