简体   繁体   中英

Unable to access notion comments via API/python

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?

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. 在此处输入图像描述

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': {}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM