In a discussion titled ‘New to API, 403 Errors’ under the Dev category, donlucas struggles with adding a member to a group via Discourse’s REST API, receiving 403 Forbidden and ‘invalid_access’ errors despite using an admin account. Falco suggests API credentials should be passed as GET parameters, not form data, though blake clarifies they can also be in the request body for POST/PUT. donlucas asks which user type can perform the action; blake advises starting with an admin, noting moderators may also work. Further testing reveals successful GET requests for site settings but continued failures on group/topic creation. blake corrects donlucas to omit topic_id when creating a new topic and to use the group’s numeric id (not name) in the PUT request. donlucas appreciates the clarity and asks how to discover group IDs, noting /admin/groups.json returns nothing. Falco explains the endpoint was moved to /groups/search.json. donlucas confirms success after retrieving group IDs via the updated endpoint and thanks both helpers.
What user type should I be using to add a member to a group via the API? If I go to /admin/users/list/active I appear to have three types of users: admin users, moderators, unprivileged.
I’ve created a few admin users and verified them against the api using a get request for site settings:
curl -X GET "https://my.discourse.com/admin/site_settings.json" -F "api_key=theGeneratedAPIKeyForanAdmin" -F "api_username=anAdmin"
This returns the site settings, However attempting to add a user to a group or even add a topic fail with the same api users.
For instance:
curl -X POST "https://my.discourse.com/posts" -H "Content-Type: multipart/form-data;" -F "api_key=theGeneratedAPIKeyForanAdmin" -F "api_username=anAdmin" -F "title=This is an API created topic" -F "topic_id=38" -F "raw=This is the end of the world as we know it, and I feel fine."
returns:
{"action":"create_post","errors":["Something has gone wrong. Perhaps this topic was closed or deleted while you were looking at it?"]}
I’ve left category, target_usernames, archetype, create_at out of the curl command as they’re optional (according to the api docs).
curl -X POST "https://my.discourse.com/posts" \
-H "Content-Type: multipart/form-data;" \
-F "api_key=theGeneratedAPIKeyForanAdmin" \
-F "api_username=anAdmin" \
-F "title=This is an API created topic" \
-F "raw=This is the end of the world as we know it, and I feel fine."
When creating a group be sure to use the id of the group and not the name of the group and to use a PUT request:
Than you for your patience, and Fantastic! I am new to discourse and managing forums, the wording for Discourse API Docs topic_id could be clearer. ‘Required when replying to topic or post.’ or similar.
In regard to group_id? How is this discoverable? In the GUI, I see the title, short_name (name) and nothing else. From the api docs, it looks like I need to GET from /admin/groups.json, but that returns no value.