I need help accessing content from my Notion pages through their API. I set up an integration and connected it to the pages I want to read from. Here’s what I’m trying:
class NotionWrapper:
def __init__(self, token):
self.token = token
self.request_headers = {
'Authorization': f"Bearer {self.token}",
'Content-Type': 'application/json',
'Notion-Version': '2022-06-28'
}
self.http_session = requests.Session()
self.http_session.headers.update(self.request_headers)
self.base_api_url = 'https://api.notion.com/v1/'
def fetch_page_content(self, target_page_id):
endpoint_url = urljoin(self.base_api_url, f"blocks/{target_page_id}/children?page_size=100")
return self.http_session.post(endpoint_url)
my_token = "secret_..."
my_page_id = "..."
notion_client = NotionWrapper(my_token)
api_response = notion_client.fetch_page_content(my_page_id)
print(api_response.json())
But I keep getting this 401 error:
{
"object": "error",
"status": 401,
"code": "unauthorized",
"message": "API token is invalid.",
"request_id": "433f38d5-d281-4b88-9bb5-875d4d0b0d8b"
}
I double checked my API token and it looks correct. The integration has proper permissions too. What am I missing here?