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?

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.

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.

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)?

게시물이 처음 작성된 이후로 API가 변경된 것 같습니다. 요즘에는 HTTP 헤더 Api-Username:…를 통해 사용자 이름을 지정해야 합니다.

안녕하세요,

저는 다른 오래된 포럼의 특정 사용자 그룹이 작성한 게시글을 현재 포럼에 가져오려고 하고 있습니다.

문서에는 API를 통해 게시글을 생성하는 방법이 설명되어 있지만, 해당 게시글이 특정 사용자의 것으로 보이도록 만드는 방법은 명확하지 않습니다. 이전에 제안된 해결책은 더 이상 작동하지 않는 것 같습니다.

사용자 이름을 입력할 수 있는 필드가 있을까요? 문서에서 찾을 수 없었습니다.

도움 주셔서 감사합니다!

마이그레이션 스크립트를 사용해 볼 생각을 해 보셨나요?

이 방법이 훨씬 간단할 수 있습니다. 이전 포럼이 어떤 플랫폼에서 운영되었는지에 따라, Meta에는 단계별 튜토리얼이 많이 올라와 있습니다.

“All Users”에 대한 API 키를 생성하면:

이후 API 요청을 보내는際に Api-Username 헤더를 통해 게시물을 생성할 사용자를 지정할 수 있습니다.

@blake님, 감사합니다. 이 길을 따르겠습니다.
@justin님, 감사합니다. 당분간은 API를 사용하는 쪽으로 가닥을 잡겠지만, 향후를 위해 import 기능도 염두에 두겠습니다!