Can't seem to create topic or post using discourse_api gem


(Nic Mc Phee) #1

I’m trying to migrate 100 or so messages from another discussion tool experiment to our newish Discourse install (which folks are generally much happier with – thanks!). I can use the discourse_api gem to connect to the install and successfully pull down things like recent posts and a list of users, but any attempts to create a topic or a post fail in a way that I can’t make sense of.

There’s no error returned or any indication of failure; I just get the 50 latest posts back with no other info, and my new topic/post never appears. Has anyone else had this experience? Am I just doing something silly? Is there some way (in Ruby, via the gem) to get the error message that I assume is getting swallowed somewhere along the way?

Many thanks in advance.

(Sam Saffron) #2

You are going to have to give much more detail here.

(Nic Mc Phee) #3

Thanks for the response, and sorry to have not provided enough – this at least suggests that this isn’t a common occurrence.

I’m trying to migrate content from Slack over to our self-hosted internal Discourse install. I’ve got some Ruby code that processes all the Slack posts and (hopefully) uploads them as Discourse posts via the discourse_api gem.

I know that we’re able to connect to and authenticate with our Discourse install; code like the following works fine:

p client.list_users('active').map { |e| e["username"] }
p client.latest_topics

When, however, I try to create a topic with code like this

  response = client.create_topic(
    category: "Slack Archives",
    skip_validations: true,
    auto_track: false,
    title: message["topic"],
    raw: message["discourse_text"]

the response variable ends up containing the 50 latest posts, starting with

{"latest_posts"=>[{"id"=>784, "name"=>"Nic McPhee", "username"=>"mcphee", ...

There’s no indication of an error except for the unexpected response, and the fact that the topic never appears in our Discourse install. Since there’s no sort of error returned, it’s not obvious to me how to best approach debugging the problem. Am I correct in assuming that other people are able to create topics and posts using the gem?

(Sam Saffron) #4

you sure you have your api key set right, are you able to write anything? are you running latest discourse?

(Nic Mc Phee) #5

It’s a brand new Discourse install (installed less than a month ago), so we should be good on our version. The API key seems good. If I change a character of it, things like list_users and latest_topics fail.

I’ve only tried writing topics and posts, and they both fail. Is there a another simple write action that would be good to try?

(Jeff Atwood) #6

Are these valid Discourse posts? More than 20 characters? Coming from Slack chat they might be pretty short.

(Nic Mc Phee) #7

Good point. Some probably are going to be on the short side. (There will be some “me too” type posts since Slack doesn’t have a “Like” button.) My guess/hope (the documentation is thin on this) was that skip_validations: true turned those checks off, though.

That said, the posts will definitely be more than 20 characters, though, because I’m adding a line at the start of each message with the name of the poster and the date & time of the original Slack posting. That alone is nearly 20 characters.

(Sam Saffron) #8

try writing something, create a category via api, or run any of the samples that write stuff. my guess is that the api key is incorrect or something.

(Nic Mc Phee) #9

I tried creating a new category:

result = client.create_category(
    name: "Test category", 
    color: "222255", 
    text_color: "552222", 
    description: "A bogus category created in trying to debug problems with the Discourse API"

and just got nil back as the result, and the category wasn’t created. So I assume something “bad” happened, but it’s unclear what.

The credentials (username and API key) do seem to be valid as I can, for example, get a list of all the users with:

client.list_users('active').map { |e| e["username"] }

If I add a spurious character to either the username or API key, then trying to get the list of users returns an empty string. This makes me think that those small changes “break” the API credentials, although just getting an empty string back isn’t a very informative “error message”.

As an aside, am I correct in reading the discourse_api code that the implementation typically assumes that all (write) operations succeed and return a “reasonable” piece of the return value? This doesn’t address my immediate question, but might it make sense to at least check the return code and do something like raise an exception if it’s not happy?

Any additional suggestions on things I might try to debug this problem?

Many thanks for your time and assistance.

(Sam Saffron) #10

Those are read operations so … well … api key does not matter for that.

(Kane York) #11

I think that it’s retrieving the /admin/users list, based on the ‘active’ parameter, which does require auth.

It’s possible that the POST requests are missing X-Forwarded-For, which would cause the homepage to be rendered.

(Nic Mc Phee) #12

I think our Discourse install requires authentication to see pretty much anything; the intent is to make it a private discussion forum in support of several research projects. So I do need a correct API key to get the list of users (hence the empty string when I provide the incorrect credentials, I assume).

Is there an easy way for me to test that on this end?

(Nic Mc Phee) #13

The source of the problem has been discovered: I was using http instead of https. It appears that something about the installation on our end requires the use of https, and I’d just been using http following the examples in the gem documentation. So arguably the problem was on our end rather than yours; apologies for the confusion and thanks for your time.

It still seems weird to me that if you use http you get the 50 most recent posts instead of some indication that there had been a problem, but I don’t know what would be involved in improving that situation.

Many thanks to all – Nic

(Sam Saffron) #14

not at all, feels to me your redirect to https logic is buggy

(Nic Mc Phee) #15

I assume you mean in our Discourse install? That seems plausible; I’ll have the admins check that.

Thanks again!