How can an API user create posts as another user?

I’m using the API of Discourse with a custom UI. I’m attempting to integrate this behind an orchestration layer that already authenticates users. I’d like to be able to more or less bypass authentication on the Discourse side of things.

We use JWT tokens, and it seems that ideally I could just pass these along to Discourse and tell it to trust them. I see that there’s some JWT related projects in the discourse GitHub organization, but there’s no documentation or usage examples, Google hasn’t been helpful, I haven’t found any relevant threads in meta, and I don’t really speak Ruby :frowning:

Is there a way I could leverage our existing authentication by just passing these tokens through to Discourse?

Failing that, as an API user, how can I create topics and posts on behalf of other, authenticated users?

「いいね!」 2

One strategy I’m exploring is creating an API user for every user in my system. Any obvious downsides to this?

You could use a master API key, and specify the username of the user you are impersonating – the master API key should always work.

「いいね!」 7

Okay, that’s interesting. How would you specify the user name? As the api_username parameter on the query string?

Yes, exactly. This is the name of the user performing the action, while the API key just proves you’re allowed to speak for that user.

「いいね!」 6

Excellent, that sounds perfect. I’ll try it out.

I’m trying to do this, but I cannot specify an arbitrary username as api_username - if I specify any username other than my (admin) account, I get a 404 from the API.

Is this post out of date, or is there some other way to achieve creating topics as another user (other than creating an API key for every user)?

I think the API has changed since the post was originally created. Nowadays you have to specify the username via the HTTP header Api-Username:…

「いいね!」 3

こんにちは、皆さん。

別の古いフォーラムから、特定のユーザーが作成した投稿を当フォーラムにインポートしようとしています。

ドキュメントには API を通じて投稿を作成する方法が記載されていますが、特定のユーザーとして投稿を表示させる方法がわかりません。以前提案された解決策はもう機能していないようです。

ユーザー名を入力できるフィールドはありますか?ドキュメントでは見つけられませんでした。

ご協力ありがとうございます!

移行スクリプトを試すことを検討されましたか?

こちらの方がはるかにシンプルかもしれません。また、旧フォーラムがどのプラットフォームで動作しているかによって、Meta には段階的なチュートリアルが多数掲載されています。

「いいね!」 3

「すべてのユーザー」用に API キーを作成した場合:

その後、API リクエストを行う際に Api-Username ヘッダーで、投稿を作成したいユーザー名を指定できます。

「いいね!」 1

@blake さん、ありがとうございます。その方向で進めようと思います。
@justin さん、ありがとうございます。当面は API に固執したいですが、将来的にはインポーターも検討します!

「いいね!」 3