jamalex / notion-py

Unofficial Python API client for Notion.so
MIT License
4.3k stars 475 forks source link

400 error querying for collection view #351

Open ericmjl opened 3 years ago

ericmjl commented 3 years ago

Hi @jamalex! Thank you for making notion-py. Really like it for custom automation with Notion.

Recently, I have encountered an HTTP 400 error when trying to query for a collection. A few code snippets and their outputs might highlight what's going on.

Client instantiation works perfectly fine:

from notion.client import NotionClient
from dotenv import load_dotenv
import os

load_dotenv()
client = NotionClient(token_v2=os.getenv("NOTION_V2_APIKEY"))

Getting page works well too:

page = client.get_block("https://www.notion.so/ericmjl/Biotech-Stack-Dive-Podcast-fc136cda60fb46129eff0f746fbc94bc")
page.children  # I can see the children blocks and collections.

However, getting collections and their views fail with the same error message. To save space, I have encapsulated them in a details tag.

client.get_collection(os.getenv("NEWSLETTER_TABLE_URL"))
client.get_collection_view(os.getenv("NEWSLETTER_TABLE_URL"))
---------------------------------------------------------------------------
HTTPError                                 Traceback (most recent call last)
/var/folders/lb/fzbrctwd2klcrwsg63vzqj0r0000gn/T/ipykernel_29726/2646176100.py in 
----> 1 client.get_collection(os.getenv("NEWSLETTER_TABLE_URL"))

~/anaconda/envs/dspn-automation/lib/python3.9/site-packages/notion/client.py in get_collection(self, collection_id, force_refresh)
    183         Retrieve an instance of Collection that maps to the collection identified by the ID passed in.
    184         """
--> 185         coll = self.get_record_data(
    186             "collection", collection_id, force_refresh=force_refresh
    187         )

~/anaconda/envs/dspn-automation/lib/python3.9/site-packages/notion/client.py in get_record_data(self, table, id, force_refresh)
    160 
    161     def get_record_data(self, table, id, force_refresh=False):
--> 162         return self._store.get(table, id, force_refresh=force_refresh)
    163 
    164     def get_block(self, url_or_id, force_refresh=False):

~/anaconda/envs/dspn-automation/lib/python3.9/site-packages/notion/store.py in get(self, table, id, force_refresh)
    184                 self.call_load_page_chunk(id)
    185             else:
--> 186                 self.call_get_record_values(**{table: id})
    187             result = self._get(table, id)
    188         return result if result is not Missing else None

~/anaconda/envs/dspn-automation/lib/python3.9/site-packages/notion/store.py in call_get_record_values(self, **kwargs)
    252                 )
    253             )
--> 254             results = self._client.post(
    255                 "getRecordValues", {"requests": requestlist}
    256             ).json()["results"]

~/anaconda/envs/dspn-automation/lib/python3.9/site-packages/notion/client.py in post(self, endpoint, data)
    258                 )
    259             )
--> 260             raise HTTPError(
    261                 response.json().get(
    262                     "message", "There was an error (400) submitting the request."

HTTPError: Invalid input.

May I ask whether you might have a hypothesis as to why getting collections and their views are failing?

Update: this issue might actually be fixed by https://github.com/jamalex/notion-py/pull/334.

sergei-shteiner commented 3 years ago

Encoutered the same issue today, the #334 did not fix it to me unfortunately.

prl-rhenares commented 3 years ago

Same here, #334 didn't seem to fix it.

JustDhan commented 3 years ago

Same here, #334 didn't seem to fix it

melino commented 3 years ago

Same here. :-(

5n7-sk commented 3 years ago

Notion has made the official API public beta, so I think some or all of the unofficial API has been deprecated.

thomashirtz commented 3 years ago

Same issue :/

ericmjl commented 3 years ago

I can confirm that the fix in #352 fixes the problem. @jamalex would you be kind enough to chime in up there to see if there is anything else that need to be done to finish up the PR?

For others, the tl;dr until the PR is merged is:

pip install git+https://github.com/jamalex/notion-py.git@refs/pull/352/merge

Btw, just wanted to also add that the Python package helped me build a hacky garage sale app backed by notion. Huge kudos to you for building notion-py, it's super useful!