Use the API to find a username based on an email?

I’m trying to integrate a Discourse forum into an existing site.

Is it possible via the API to find out what a user’s username is, if all I have is their email?

2 Likes

In lieu of a bump, let me describe what I want to do.

On my CMS, I have a bunch of user profiles. On each profile I want to display that user’s activity on Discourse, which I would get via RSS.

For example, my activity on this Discourse is available here:
https://meta.discourse.org/users/alehandrof/activity.rss

To get this URL I need a username, but I don’t have this data at the CMS. All I have is users’ email, which is the same in both the CMS & Discourse. (I’ve made sure of this manually for now, since it’s a small community, but I will be implementing SSO in the near future.)

It’s a PHP CMS and I’ll write a small add-on that pings Discourse with the email and gets a username in return. I will then use the username to form the URL and give it to a plugin that will consume the RSS feed and allow me to output it with my own HTML.

Is there an API endpoint that allows me to find a user’s username through their email?

There’s a function to do this in this PHP API: https://github.com/communiteq/discourse-api-php

(The PHP APIs are a bit all over the place. There are some newer forks out there, but I ran into trouble with the one I tried.)

1 Like

There is nothing magical here, just do the action in your admin web UI and watch the f12 network console in the browser. Those are the API calls necessary.

I don’t follow :frowning:

The API I linked to works by filtering (which I didn’t know what possible) the list of active users by their email. Not sure how I’d trigger this in thew web UI.

function getUsernameByEmail($email)
{
    $users = $this->_getRequest("/admin/users/list/active.json?filter=".urlencode($email));
    foreach($users->apiresult as $user) {
        if($user->email === $email) {
            return $user->username;
        }
    }
    return false;
}

BTW, let me clarify something in my previous response: I did get the API I linked to to work just fine. I just had trouble with other forks of it.

One thing I’d like to update during SSO is the user’s website field. I tried changing it in the admin to see what happens in the Network tab, but I don’t understand what I’m looking for. (AFAICT, I can’t add the website to the SSO payload, but I should be able to add another step that changes the website right before submitting the payload.)

Per:

https://github.com/discourse/discourse/commit/06365023c4f215bb6867efe7e41bf740c919586b

If you are searching by email use:

SITENAME.com/admin/users/list/all.json?email=some@email.com

Using filter is inefficient and not recommended for this use case!

12 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.