How to fetch Notion page data using API

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?

You’re using the wrong HTTP method. Switch from POST to GET for fetching block children - change self.http_session.post(endpoint) to self.http_session.get(endpoint). I hit this same issue when I started with Notion’s API. The error makes it seem like your token’s bad, but it’s just the wrong method. Also double-check your page ID format - no dashes or extra characters. Those can cause auth issues too.

Had the same authorization nightmare with Notion API. Your token looks fine, but double-check you copied the full integration secret from notion.so/my-integrations - sometimes extra whitespace sneaks in. More importantly, did you actually share the page with your integration? Creating the integration isn’t enough. You’ve got to invite it to each page or database manually. Hit the three dots on your page, go to connections, and add your integration. That 401 error is confusing because it’s usually a permissions issue, not a bad token.

double-check your page_id format - notion uses both short and long formats, so try converting it to proper uuid format first. also make sure you’re not mixing up page ids with database ids since they work differently with the api. This exact thing got me before when i copy/pasted from browser urls.