I’m working with the Notion API to pull content from my workspace pages. I set up an integration and linked the pages to it, but I keep getting a 401 unauthorized error when I run my code.
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, target_page_id):
endpoint = urljoin(self.BASE_URL, f"blocks/{target_page_id}/children?page_size=100")
return self.http_session.post(endpoint)
api_token = "secret_..."
target_id = "..."
api_client = NotionAPI(api_token)
result = api_client.fetch_page_blocks(target_id)
print(result.json())
The response I get is:
{
"object": "error",
"status": 401,
"code": "unauthorized",
"message": "API token is invalid.",
"request_id": "433f38d5-d281-4b88-9bb5-875d4d0b0d8b"
}
I double checked my token and it should be valid. The pages are connected to my integration. What could be causing this authentication issue?