I am trying to use the Discourse API to create a list of all posts (first posts only) with a specific tag. So far, I’ve only been able to get the latest posts, which retrieves around 40 posts, including comments. When I tried retrieving a specific post id, some of them have worked, but for some I got the response
{'errors': ['You are not permitted to view the requested resource.'],'error_type': 'invalid_access'}
even though the API key I’m using has admin access to all posts on our Discourse. Has anyone encountered this issue before?
AI-generated summary
The discussion revolves around issues with retrieving posts using the Discourse API. smejak is trying to use the API to create a list of first posts only with a specific tag, but encountering permission errors despite using an admin API key. RGJ suggests posting the code snippet to investigate further.
smejak shares the code and mentions that they can retrieve individual posts if they have been in the list originally retrieved by posts.json. However, they struggle to retrieve posts by ID.
RGJ clarifies that the number in the URL is not the post ID, but rather the post number relative to the topic ID. They provide an example of how to retrieve the post ID from the topic JSON.
smejak is now able to retrieve posts by ID and asks how to retrieve the latest topics and the raw text for the first post in each topic, specifically inquiring about the List Topics call in the API documentation.
Can you post your code snippet? (Without including the API key)
Do you see any pattern here in which work and which do not?
Hi @RGJ, I noticed I’m able to retrieve individual posts if they have been in the list originally retrieved by posts.json. I used the code snippet below
url = "https://forum.algovera.ai/posts.json"
username = "algovera"
params = {'Api-Key': api_key, "Api-Username": username}
r = requests.get(url = url, params = params)
data = r.json()
Then I tried changing this call to https://forum.algovera.ai/posts/{id}.json", where the ID was taken from the URL of the particular post I was looking at.
The number in the URL is not the ID, that’s the post number, which is relative to the topic ID.
If you want to get the post ID you can retrieve it from the topic json as post_stream.posts[].id
So the posts in this topic have
| topic ID | post_number | ID |
|---|---|---|
| 255455 | 1 | 1241013 |
| 255455 | 2 | 1241026 |
| 255455 | 3 | 1241056 |
and your most recent post in this topic is https://meta.discourse.org/posts/1241056.json
Oh right, thank you. That worked, so what would I need to call if I want to retrieve the latest topics and then the raw text for the first post in each topic? I saw the List Topics call in the docs but I’m not sure what the slug should be.