Forum Discussion

khill's avatar
khill
Mentor
7 years ago

REST API V2 - Query email from users returns null value

Hi everyone,

I am trying to make an API v2 call to return a list of user IDs and their email addresses. When I make the call the login returns correctly, but email field returns with no data.

Any ideas?

Thanks!

Kate

 

Example LiQL:

SELECT login, email FROM users ORDER BY registration_data.registration_time DESC

I am using the standard V2 link format:

https://api.lithium.com/community/2.0/[mycommunity]/search?q=[LiQL query]&client_id=[clientid]

Sample Results:

 "data" : {
    "type" : "users",
    "list_item_type" : "user",
    "size" : 2000,
    "items" : [ {
      "type" : "user",
      "login" : "uid1",
      "email" : ""
    }, {
      "type" : "user",
      "login" : "uid2",
      "email" : ""
    }, {
      "type" : "user",
      "login" : "uid3",
      "email" : ""

 

  • Parshant is right, would you like your email to be accessible like that trough a public community URL =)?

    You have two options (maybe more, but that's off the top of my head)

    a) Do the OAuth flow with the public v2 API URL (the one you're using now)

    or

    b) Write a custom component that does the same thing, but uses restadmin() instead, like this even if the current user does not have the right permissions, the personal data will be fetched, that also means you should be careful using restadmin() as it can expose information that you might not want to, so limit your query fields etc., that might look something like this:

    <#assign query = "SELECT login, email FROM users ORDER BY registration_data.registration_time DESC"?url />
    <#assign response = restadmin("2.0", "/search?q=" + query) />
    
    <#if response.data.items?has_content>
        <ol class="userlist">
        <#list response.data.items as user>
            <li class="user">
                <span class="user__login">${user.login}</span>
                <span class="user__email">${user.email}</span>
            </li>
        </#list>
        </ol>
    </#if>

    this is untested code, so use at your own risk =)

3 Replies

  • khill,

    It only happens when you are not logged in or not authorized to use the API v2.

    Check if you are passing the correct client_id to fetch the user record.

    Email is the personal information of the users and can only be access if you have certain permission to call the API to fetch the response.

  • Parshant is right, would you like your email to be accessible like that trough a public community URL =)?

    You have two options (maybe more, but that's off the top of my head)

    a) Do the OAuth flow with the public v2 API URL (the one you're using now)

    or

    b) Write a custom component that does the same thing, but uses restadmin() instead, like this even if the current user does not have the right permissions, the personal data will be fetched, that also means you should be careful using restadmin() as it can expose information that you might not want to, so limit your query fields etc., that might look something like this:

    <#assign query = "SELECT login, email FROM users ORDER BY registration_data.registration_time DESC"?url />
    <#assign response = restadmin("2.0", "/search?q=" + query) />
    
    <#if response.data.items?has_content>
        <ol class="userlist">
        <#list response.data.items as user>
            <li class="user">
                <span class="user__login">${user.login}</span>
                <span class="user__email">${user.email}</span>
            </li>
        </#list>
        </ol>
    </#if>

    this is untested code, so use at your own risk =)

  • khill's avatar
    khill
    Mentor
    7 years ago

    Ah okay so passing the client ID was not enough to authenticate as I was hoping! (I've had troubles with the OAuth process so am trying to avoid it :smileyhappy:).

     

    Thanks to you both and I will proceed with the above suggestions!

    Kate