A user is trying to post a new topic via the Discourse API ruby gem as a non-staff user but encounters a DiscourseApi::NotFoundError (404). The issue is resolved when the user uses the category ID instead of the category name. tshenry initially struggles with the error, but blake explains that regular users require the category ID for the API to work, as the category name lookup fails for non-moderators. After switching to the category ID, tshenry confirms the solution works.
I’m trying to use the following to post a new topic as a non-staff user via the Discourse API ruby gem:
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
require File.expand_path('../../lib/discourse_api', __FILE__)
client = DiscourseApi::Client.new("https://www.example.com")
client.api_key = "GLOBAL_KEY"
client.api_username = "ADMIN_USERNAME"
topic = client.create_topic (
category: "Category",
skip_validations: true,
auto_track: false,
title: "This is a test topic",
created_at: "Sat Jul 22, 2016 5:00 pm GMT-0700",
api_username: "NORMAL_USER_USERNAME",
raw: "This is just a test"
)
When I run the above, I get:
/Users/me/discourse_api/lib/discourse_api/client.rb:147:in `handle_error': {"status"=>404, "error"=>"Not Found"} (DiscourseApi::NotFoundError)
from /Users/me/discourse_api/lib/discourse_api/client.rb:136:in `request'
from /Users/me/discourse_api/lib/discourse_api/client.rb:87:in `post'
from /Users/me/discourse_api/lib/discourse_api/api/topics.rb:12:in `create_topic'
from test.rb:11:in `<main>'
There are no restrictions on the category. And I can confirm that the normal user is able to post a new topic through the actual site.
Interestingly, if I grant the normal user moderation power, everything works fine - the topic posts. This seems to imply there is a permission issue going on somewhere, but I’m having trouble tracking it down.
It also might be worth noting that I am able to post a reply to an existing topic as the normal user using create_post() without granting them moderation power.
Unfortunately it’s the same situation. If I run it after I’ve granted the normal user moderation power, it goes through fine, but as a normal user without moderation power, I still get the DiscourseApi::NotFoundError
Unfortunately my /logs are clean. Not even a mention of the 404 there. I’m not sure how relevant this is, but just in case, I’m trying to create the new topic on a test production environment.
I haven’t been able to track down any other errors outside of the one I posted that displays in the console when I try to run the ruby file.
Out of curiosity, have you attempted to repro using the scenario I’ve laid out? I may just try a new, clean instance and rule things out one at a time. Maybe there’s some site setting or configuration that’s getting in the way.
I’ve been running it in a development environment, but just tested it in a production env. The logs don’t show for me either, but I’m able to replicate the same behavior is both env’s.
If you create a post as an admin or moderator and you specify the category as a string it does its best to guess the correct category and will work, but a regular user will hit this line
causing a 404 if you don’t specify the category as an id.
Awesome! I really appreciate you taking the time to look into this.
I will give the category ID a try again when I get home and make sure I have all the arguments in place exactly like your example. When I originally tried with the category ID, I may have have accidentally left something out after trying various things.