New to API, 403 Errors

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?

1 Like

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

3 Likes

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.

1 Like

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.

1 Like

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"
4 Likes

Than you for your patience, and Fantastic! I am new to discourse and managing forums, the wording for http://docs.discourse.org/#tag/Topics%2Fpaths%2F~1posts%2Fpost 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.

2 Likes

We moved this route to /groups/search.json

4 Likes

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

Thank you both!

2 Likes