I'm trying to retrieve the content of my Notion workspace using the Notion API. I created a new integration, made sure pages I'm trying to access are connected to the integration and executed the following code:
class NotionClient:
def __init__(self, access_token):
self.access_token = access_token
self.headers = {
'Authorization': f"Bearer {self.access_token}",
'Content-Type': 'application/json',
'Notion-Version': '2022-06-28'
}
self.session = requests.Session()
self.session.headers.update(self.headers)
self.NOTION_BASE_URL = 'https://api.notion.com/v1/'
def query_workspace(self, page_id):
page_url = urljoin(self.NOTION_BASE_URL, f"blocks/{page_id}/children?page_size=100")
return self.session.post(page_url)
access_token = "secret_..."
page_id = "..."
notion = NotionClient(access_token)
response = notion.query_workspace(page_id)
print(response.json())
This gives the 401 error code and returns the following error:
{"object": "error",
"status": 401,
"code": "unauthorized",
"message": "API token is invalid.",
"request_id": "433f38d5-d281-4b88-9bb5-875d4d0b0d8b"
}
I'm not sure what is the problem here, I'm certain my API key is valid and has the required scope and this should be the current Notion version as far as i know, but i could be wrong.
You have to use get()
instead of post()
. That's all.
return self.session.get(page_url)