How to fetch Notion page data using API

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?

Had the same problem when I started with Notion’s API. Here’s something nobody mentioned yet - check your page ID format. When you copy it from the browser URL, it’s usually messed up. Remove all the dashes and make sure it’s exactly 32 characters, nothing else. Also double-check your integration token starts with “secret_” and doesn’t have extra spaces when you copied it from the settings page.

hey, try using GET instead of POST for the fetch_page_content method. the notion API for blocks needs a GET request. that’s prob why you’re hitting that 401 error, even if your token seems right.

Yeah, the GET vs POST thing is right, but there’s another gotcha that trips people up with 401 errors. When you make an integration in Notion, you have to manually share each page or database with it. Just having the token doesn’t cut it - you’ve got to invite the integration to each specific page you want to access. Go to your page, hit the three dots, pick “Add connections” and select your integration. I made this exact mistake when I started with Notion’s API and wasted hours thinking it was a token issue when it was just permissions.