Discourse API. Searching users by email


(Alex Nikitin) #1

Is any way in API search users by email?

I make some kind of SSO feature for my site. Discourse SSO feature working good, but I want when user login on my main site he login on my discourse site too. I try to make it by API. But have a problem in search user by his email.

As I understand I cannot get email of user by API (I try this: /users/search/users.json and have no email in result)
And when I make this search: /admin/users/list/active.json
I receive resuls like this (no paging in results disapointed)

0 => stdClass#2
        (
            [id] => 100
            [username] => 'xxx'
            ...
            [single_sign_on_record] => stdClass#3
            (
                [user_id] => 100
                [external_email] => 'xxx@gmail.com'
        ...
            )
        )
...
99 => ...

P.S. Only 100 results? How to get other?

So in [single_sign_on_record] I can see user email.
The Question: Is any way to get single user by searching by his [external_email] or better by his [email]?

Thanks for all answers :slight_smile:


(Michael - DiscourseHosting.com) #2

There is an example in our PHP API Client

EDIT: I think you need to add show_emails=true to the query string nowadays.

You still need to loop through the results and compare with your search query because searching for michael@gmail.com will also return mailmichael@gmail.com and other supersets of what you are looking for.


(Alex Nikitin) #3

Thank you very much for your answer!

That is working for me (may be help for future diggers like me :)):

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

(Joshua Frank) #4

Does this still work for you? I was doing it this way but some recent version of Discourse made it stop working, and now I always get an empty array.


(Michael - DiscourseHosting.com) #5

I’m surprised this ever worked at all. I fixed it and actually saw it work this time :smile:

There is a new version on Github now.


(Tuan) #6

Hi Michael, I tried the lib at GitHub - discoursehosting/discourse-api-php: PHP API client for Discourse

I’ve put in my credentials but when I execute the PHP, I get a response of

Class Object ( [http_code] => 0 [apiresult] => ) stdClass Object ( [http_code] => 0 [apiresult] => ) stdClass Object ( [http_code] => 0 [apiresult] => ) stdClass Object ( [http_code] => 0 [apiresult] => ) stdClass Object ( [http_code] => 0 [apiresult] => )

Any ideas of what I am doing that’s wrong?

Thx


(Michael - DiscourseHosting.com) #7

http_code zero, that looks like you’re not even hitting your site. What does your config look like?


(Tuan) #8

Hi Michael, I have the following (removed the passwords of course)
In DiscourseAPI.PHP

private $_protocol = ‘http’;
private $_apiKey = ‘My API key from the USER settings’;
private $_dcHostname = ‘tqdesign.trydiscourse.com’;
private $_httpAuthName = ‘tqdesign@gmail.com’;
private $_httpAuthPass = ‘mypassword’;

in Example.php

$api = new DiscourseAPI(“http://tqdesign.trydiscourse.com”, API_KEY, API_HOST_PROTOCOL);


(Michael - DiscourseHosting.com) #9

You should not edit DiscourseAPI.php, all parameters are passed to the constructor.

All the parameters should be entered in the constructor function call and the hostname should not include http://
API_KEY and HOST_PROTOCOL should be replaced with your settings.

$api = new DiscourseAPI('tqdesign.trydiscourse.com','My API key from the USER settings', 'http');


(Blake Erickson) #10

@Tuan Were you able to get this to work? Do you get a different error message this time?

I was able to get this to work on a fresh ubuntu 16.04 LTS install by doing:

  1. sudo apt install php
  2. sudo apt install php-curl
  3. sudo apt install unzip
  4. wget -O php-api.zip https://github.com/discoursehosting/discourse-api-php/archive/master.zip
  5. unzip php-api.zip
  6. cd discourse-api-php-master/
  7. create an example2.php file that look like this:

example2.php file:

<?php

require_once "lib/DiscourseAPI.php";

$api = new DiscourseAPI("tqdesign.trydiscourse.com", "your-secret-api-key", "http");

// in order to activate we need the id
$r = $api->getUserByUsername('system');
print_r($r);

Now run php example2.php and you should see a successful output.


(Tuan) #11

@blake - thanks for your help. I am now getting a 403 so my api key must be a problem as well. Isn’t the key value the Dashboard, API and then the API key? There are two rows. One is All Users and the other is one for my account. Apologies for the elementary questions. Just getting started with Discourse.

Thanks again.


(Blake Erickson) #12

Try the api key for all users and see if you get the same error.


(Tuan) #13

Same results with All Users credentials.


(Tuan) #14

Finally figured it out. Recreated the API key. Copied it in and everything works now. Thanks @blake and @michaeld for all of your help! Much appreciated.


(Jeff Atwood) #15

(Sam Saffron) #16

Per:

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!