I want to get content from my Notion pages through their API but I keep getting unauthorized errors. I set up an integration and connected it to my pages but something is still wrong.
class NotionAPI:
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_url = 'https://api.notion.com/v1/'
def fetch_page_blocks(self, block_id):
endpoint = urljoin(self.base_url, f"blocks/{block_id}/children?page_size=100")
return self.http_session.post(endpoint)
my_token = "secret_..."
my_page_id = "..."
api_client = NotionAPI(my_token)
result = api_client.fetch_page_blocks(my_page_id)
print(result.json())
I get this error back:
{
"object": "error",
"status": 401,
"code": "unauthorized",
"message": "API token is invalid.",
"request_id": "433f38d5-d281-4b88-9bb5-875d4d0b0d8b"
}
My token should be correct and I made sure the integration has access to the pages. Not sure what I am missing here. Any ideas what could be wrong?