How to fetch Notion page data using API

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?

Had the same frustrating experience last month integrating Notion into my project management setup. That 401 error is misleading - your token’s probably fine. Notion’s API throws ‘unauthorized’ instead of ‘method not allowed’ when you use POST on a GET endpoint (security thing). Fix the HTTP method to GET first. Also check your page ID is in UUID format without hyphens. If you grabbed it from the browser URL, extract just the 32-character hex string. And make sure your integration has read permissions for content, not just page connection.

That authentication error is hiding the real problem. Your token format and headers look fine, but you’re using the wrong HTTP method. You’re sending a POST request to get data, which breaks REST rules and makes Notion’s API throw a 401 instead of a proper method error.

I hit this exact same issue when I started with Notion’s API. The blocks endpoint wants GET requests for retrieving data. Switch your fetch_page_blocks method to use self.http_session.get(endpoint) instead of POST. Also, double-check your page ID doesn’t have dashes if you grabbed it from the URL. Fix the HTTP method and your auth should work fine.

you’re using POST instead of GET to fetch the blocks. Notion’s block endpoint needs GET requests, not POST. change self.http_session.post(endpoint) to self.http_session.get(endpoint) and that should fix your auth error.