When I use the API to get the Discourse user object, it works fine, but I don’t see the external_id included in there. If I have a Discourse user ID or username, how can I get the external_id from that?
סיכום מבוסס בינה מלאכותית
The discussion revolves around retrieving the external_id from a Discourse user object via the API. pnoeric initially couldn’t find the external_id in the user object when using the /users/username.json endpoint. blake clarifies that the /admin/users/:id.json endpoint includes the single_sign_on_record, which contains the external_id, unlike the non-admin /users/ endpoint. pnoeric notes the inconsistency in API behavior—using /users/by-external/id.json doesn’t return the single_sign_on_record, and the admin endpoint requires the Discourse ID while others use the username. blake explains this is by design, as the admin endpoint returns more data for security and UI rendering purposes. pnoeric acknowledges the inconsistency but appreciates the API’s strength. riking points to a guide on reverse-engineering the API for further clarity.
Aha - I was calling /users/blake.erickson.json (no /admin) which also returns a user object, but without single_sign_on_record.
Hey, why is it that the /users/ endpoint will take (only?) a username, but if you want to get a user by their Discourse ID, then it’s /admin/users/:id.json? Just quirks of the API design?
Further weirdness: if I retrieve a user by their external_id, calling /users/by-external/:id.json, then I don’t get the single_sign_on_record
So the API has some quirks… sometimes a user object has the single_sign_on_record properties, sometimes it doesn’t ![]()
By design the admin endpoint will return more data than the non-admin endpoint. Yes, they are both fetching “user” records, but it would be super bad if the /user/... endpoint that regular users can call returned the same thing as /admin/user/....
Aha, I didn’t realize the /user endpoint would be available to individual users. I forgot that there is such a thing as User API keys.
I keep getting thrown by inconsistencies. Right now, if I want single_sign_on_record, I have to know the Discourse user ID (/admin/users/:username.json doesn’t work)… but most other API calls seem to require the username (like /users/:username.json).
And then there are separate API endoints that are very similar and seem almost identical but return different results, i.e. /admin/users gives me single_sign_on_record data for the user, but calling just /users/ does not.
None of this is a big deal, I’m figuring it out as I go… once I get further along, I might see if I can tweak the docs a bit and make some of this more clear.
The distinction here comes from the fact that the endpoints return the data needed to render the pages in the UI. Take a look here:
Sure, I totally get it-- and I’ve been using that trick to monitor all those XHR requests. It’s all good. It’s just… inconsistent.
But I am endlessly grateful that Discourse has such a strong API!
