Unable to access notion comments via API/python

225 Views Asked by At

I'm trying to read the comments on a database entry in notion but I can't figure out how I need to make the request.

import requests
_url = 'https://api.notion.com/v1/comments'
_headers = {"Authorization": _auth,
            "Notion-Version": "2021-08-16"} 
_data = {'block_id': _page_id}  
_results = requests.post(_url, headers=_headers, data=json.dumps(_data))
_data = _results.json()

These results I get back are something like this:

{u'code': u'validation_error',
 u'message': u'body failed validation. Fix one:\nbody.parent should be defined, instead was `undefined`.\nbody.discussion_id should be defined, instead was `undefined`.',
 u'object': u'error',
 u'status': 400}

I'm trying to follow the docs here https://developers.notion.com/reference/retrieve-a-comment but I'm not sure how it translates into python.

Has anyone managed to do this?

1

There are 1 best solutions below

0
On

If you are reading/getting data from the API, you should use GET, not POST.

To translate the API to Python code, you will do something like this. Notice that the block id is a query parameter not in the request body.

_url = 'https://api.notion.com/v1/comments'
_headers = {"Authorization": _auth,
            "Notion-Version": "2021-08-16"} 
_query = {'block_id': _page_id}  
_results = requests.get(_url, headers=_headers, params=_query)
_data = _results.json()

You also need to ensure that you have added the integration to the notion page. enter image description here

Run the code, you will get the response like this, (in my case, I don't have any comments on the page).

{'object': 'list', 'results': [], 
 'next_cursor': None, 'has_more': False, 
 'type': 'comment', 'comment': {}}