Can't create topic with tag using admin API due to insufficient trust level

AI-generated summary

cappslock reported an inability to create topics with tags via the Discourse Admin API, receiving a NoMethodError when using a single tag parameter and having tags ignored when using tags[]. Although tagging was enabled in the UI, the API calls failed to apply tags to the newly created topics. neil initially suggested verifying that tagging was enabled and provided a working curl example using the tags[] parameter format. After cappslock confirmed tagging was active and provided their specific commit hash and request details, neil verified the syntax appeared correct. The investigation shifted to user permissions when cpradio and neil suggested checking the minimum trust level required to tag or create tags. cappslock realized the issue was that the api_username was set to a user with insufficient trust level (the default setting requires Trust Level 3 to create tags), rather than an admin account. Adjusting the trust level or using an admin account resolved the issue.

I have not been able to create a topic with a tag using the admin API (i.e. when I include api_key and api_username on the query string).

POSTing to /posts, I attempted to use the tags[] param I see in the UI’s calls to the API, which were happily ignored. I also tried to post with a tag param which caused the server to 500:

NoMethodError in PostsController#create
undefined method `-' for "testing123":String Did you mean? -@

Extracted source (around line #163):
161
162
163
164
165
166
              
    staff_tags = SiteSetting.staff_tags.split("|")

    tag_diff = tags - staff_tags
    tag_diff = tags - tag_diff

    tag_diff.present? ? tag_diff : nil

Rails.root: /vagrant

lib/discourse_tagging.rb:163:in `staff_only_tags'
lib/topic_creator.rb:28:in `valid?'
lib/has_errors.rb:10:in `validate_child'
lib/post_creator.rb:93:in `valid?'
lib/post_creator.rb:124:in `create'
lib/new_post_manager.rb:145:in `perform_create_post'
lib/new_post_manager.rb:119:in `perform'
app/controllers/posts_controller.rb:142:in `block in create'
lib/distributed_memoizer.rb:28:in `memoize'
app/controllers/posts_controller.rb:141:in `create'
config/initializers/100-quiet_logger.rb:13:in `call_with_quiet_assets'
config/initializers/100-silence_logger.rb:26:in `call'
lib/middleware/missing_avatars.rb:21:in `call'
lib/middleware/turbo_dev.rb:33:in `call'

Request

Parameters:

{"title"=>"sdaf dsfa sdfa sfd sfda sfd sfd sfd sfda ",
 "raw"=>"wr  awd wa w adaw aw dn lef galgj ",
 "tags"=>"testing123",
 "api_key"=> (scrubbed),
 "api_username"=>"user8"}

Using the API, is there a way to create a topic with a tag? Failing that, is there a way to add a tag after the fact?

Did you enable tagging? Check your settings in admin. It’s disabled by default.

You can add tags by editing the topic:

If you don’t see the tag input field, then you haven’t enabled tagging.

Tagging is enabled, and I have topics with tags that have been created via the UI. I can do this, and it works, but when calling the same API with api_key and api_username in lieu of a user token, it doesn’t work.

This worked for me and added two tags:

curl --data "api_key=asdf...asdf&api_username=neill&title=this%20one%20has%20two%20tags&raw=and%20it%20is%20twice%20as%20delicious&tags[]=mazda&tags[]=protege" http://localhost:3000/posts.json

So I’m not sure why it isn’t working for you…

No dice. I’m putting the api_key and api_host on the query string, not the body? But I tried to move them and yeah, same result. To be clear, the topic is created, but there aren’t any tags on it.

What commit are you on? Maybe it’s a quirk with the version I’ve checked out? I’m at b1fb1831df5f48aac22d76ad75d6f12edeaf9c03 on the beta branch.

Show me the curl command that isn’t working. I’m not clear if your creating a new topic (POST), updating one (PUT), or adding a reply (can’t add tags to topics from a reply).

Thanks for the help. Here’s what I’m sending:

curl -X POST -H "Cache-Control: no-cache" -H "Postman-Token: b22a11c2-5b79-e4e5-2c8c-4c8423e5e76b" -H "Content-Type: application/x-www-form-urlencoded" -d 'title=whatissobadaboutthistitle&raw=asdfasdfasdfasdfweiurasdfkjbawerad&tags[]=thisisatest&tags[]=thisisanothertest&api_key=...&api_username=user8' "http://localhost:4000/posts.json"

That looks right. Does user8 have permission to tag topics? min trust level to tag topics

Also check the TL required for creating tags?

Ugh, no, of course not. Minimum trust level to create was set to 3 (the default, I think). Early on I had thought of the admin as being the one creating tags and so assumed it would work, but of course setting api_username to arbitrary users changing that makes sense.

Thank you both very much for the help.