Notion APi HTTPError:403 客户端错误:禁止 url:https://www.notion.so/api/v3/getUploadFileUrl

And*_*Lee 2 python file-upload http-error notion-api

我目前正在尝试非正式地连接到 Notion 主页来上传图像。使用非正式 API 的原因是它不允许我们从本地文件夹上传图像。

话虽如此,我使用 token_v2 成功连接到我的主页,并使用Children.add_new('image')创建了一个图像块。然而,当我尝试上传图像时,遇到了 403 客户端错误。

几天来我一直在努力解决这个问题,所以如果我遗漏了什么,请告诉我。下面是我的代码。

from notion.client import NotionClient

def uploadEvaluationJPG():
    token_v2 = secret.notion_API("token_v2")
    client = NotionClient(token_v2=token_v2)
    # connect page
    url = 'https://www.notion.so/Home-******************************'
    page = client.get_block(url)
            
    newchild = page.children.add_new('image')
    newchild.upload_file(r"C:\NotionUpdate\progress\jpg files\Monthly Evaluation\month.jpg")
    newchild.move_to(page.children[1],"before")
    page.children[0].remove()  
Run Code Online (Sandbox Code Playgroud)

错误代码

Traceback (most recent call last):

  Input In [8] in <cell line: 11>
    newchild.upload_file(r"C:\NotionUpdate\progress\jpg files\Monthly Evaluation\month.jpg")

  File ~\AppData\Roaming\Python\Python39\site-packages\notion\block.py:641 in upload_file
    data = self._client.post(

  File ~\AppData\Roaming\Python\Python39\site-packages\notion\client.py:265 in post
    response.raise_for_status()

  File ~\AppData\Roaming\Python\Python39\site-packages\requests\models.py:909 in raise_for_status
    raise HTTPError(http_error_msg, response=self)

HTTPError: 403 Client Error: Forbidden for url: https://www.notion.so/api/v3/getUploadFileUrl
Run Code Online (Sandbox Code Playgroud)

Pau*_*laF 8

代码是正确的,Cloudflare 似乎在请求到达 Notion 之前阻止了它们。

如果我们使用云代理在 Postman 中运行请求,我们会收到成功响应 200。但是,如果选择桌面代理,我们会收到 403 Forbidden 消息。此外,我们还可以预览其原因。它说:

我们正在检查您的浏览器,请稍候...

因此,Notion/Cloudflare 似乎不喜欢请求的来源。

Notion现在有官方API,但遗憾的是文件上传尚未实现,我们将不得不等待。如果加强安全性,许多其他事情可能会在非官方 API 中开始失败。

要在 Postman 中复制:

网址:https: //www.notion.so/api/v3/getUploadFileUrl

身体:

{"bucket":"secure","name":"envs.toml","contentType":"text/plain; charset=utf-8"}
Run Code Online (Sandbox Code Playgroud)

为了进行身份验证,请创建一个名为 cookie 的标头,其值为token_v2=your_token