Notion APi HTTPError: 403 Client Error: Forbidden for url: https://www.notion.so/api/v3/getUploadFileUrl

Viewed 378

I am currently trying to informally connect to the Notion homepage to upload an image. The reason for using informal API is that it does not allow us to upload images from our local folders.

With that said, I successfully connected to my homepage using token_v2 and created an image block using children.add_new('image'). However, when I tried to upload an image, I ran across 403 Client Error.

I've been trying to solve this issue for a few days so please let me know if there's anything I'm missing. Below is my code.

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()  

Error Code

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
1 Answers

The code is correct, it appears that Cloudflare is blocking the requests before they reach Notion.

If we run the request in Postman using the cloud agent, we get a successful response 200. Choosing the desktop agent, though, we get the 403 Forbidden message. Furthermore, we can also preview the reason for it. It says:

Please stand by, while we are checking your browser...

So it seems that Notion/Cloudflare doesn't like the origin of the request.

Notion has an official API now, but sadly file upload is not implemented yet and we will have to wait for it. Many other things may start failing in the unofficial API if they tighten security.

To replicate in Postman:

url: https://www.notion.so/api/v3/getUploadFileUrl

body:

{"bucket":"secure","name":"envs.toml","contentType":"text/plain; charset=utf-8"}

For authentication, create a header called cookie with the value token_v2=your_token

Related