New to API, 403 Errors

AI-generated summary

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.

Trying to add a member to a group and not finding a solution in the meta forums.
(using python’s requests module)

requests.put('https://my.discourse.com/groups/the_group/members.json', data='{"usernames": "newForumUser", "api_key": "theGeneratedAPIKeyForUserNamedanAdmin", "api_username": "anAdmin"}', headers={"Content-Type":"application/json"}

I get a 403 Forbidden response from the server. The api_username is a site admin. If I try the command using curl:

curl -X POST "https://my.discourse.com/groups/the_group/members.json" -H "Content-Type: multipart/form-data;" -F "api_key=theGeneratedAPIKeyForUserNamedanAdmin" -F "api_username=anAdmin" -F "usernames=newForumUser"

the html returned is Oops! That page doesn’t exist or is private.

I must be missing something obvious from http://docs.discourse.org?

I believe that the api_key and api_username shoud go as GET params and not as form data.

I’ve tried to construct the entire request as a GET:

https://my.discourse.com/groups/the_group/members.json?api_key=theGeneratedAPIKeyForUseranAdmin&api_username=anAdmin&usernames=newForumUser

I receive:
{"errors":["You are not permitted to view the requested resource."],"error_type":"invalid_access"}

Thanks for responding so quickly. If I attempt a purely GET command this is what I am returned:

{"errors":["You are not permitted to view the requested resource."],"error_type":"invalid_access"}

The api_username is an admin user,

You can’t use any admin with any api_key, are you using a correct username & key pair.

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.

If you are making a put/post request they can also go in the body.

I would make sure you can get it working as an admin first, but moderators should be able to do that I think.

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).

Do not specify a topic_id when creating a topic:

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:

curl -X PUT "https://my.discourse.com/groups/group_id/members.json" \
-H "Content-Type: multipart/form-data;" \
-F "api_key=theGeneratedAPIKeyForUserNamedanAdmin" \
-F "api_username=anAdmin" \
-F "usernames=newForumUser"

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.

We moved this route to /groups/search.json

Many thanks! I can list the groups out and figure out the id from there.

Thank you both!