Forum Discussion

JasonMi's avatar
JasonMi
Lithium Alumni (Retired)
10 years ago
Solved

Lithium Avatar API Pulls

 We have another department that would like to sync customers' community avatars with their profile page. I believe the api field is here: https://community.lithium.com/t5/Community-API/bd-p/developers-rest-api?branch=AvatarImage

 

Currently users have a profile page (not hosted in Lithium). They would like add the Lithium Avatar feature to their page. They would like to pull the same avatar that they are using on the community and display it on our client profile page.  What is the proper API calls to pull the avatars from the community to the profile page? Can this be done if the profile page is not hosted on Lithium?

  • JasonMi - Yes, you can do that, but for this you'd require atleast one common parameter which could pull the corresponding avatar for you.

     

    Workflow:

     

    On the Lithium instance, you can create an endpoint which would consume the common parameter from your external page, as the ID and login name would not be available in the external site, so I'd suggest on using email in the API. The same call can be used by passing the user email, which I am sure be the common parameter b/w your external site and the community.

     

    e.g. https://community.lithium.com/restapi/vc/users/email/[your_email_here]/profiles/avatar

     

    White list the external site domain on your community instance and make a REST call on you external site to the lithium endpoint, pass the email ID, fetch the avatar.

     

    I hope this helps.

17 Replies

  • luk's avatar
    luk
    Boss
    10 years ago

    skylinegtr Could it be, that you were logged in as admin when you tried to make that call (and your other guy wasn't)?

     

    It's somewhat obscure what requests to the REST API (v1 or v2) do need to be done with the restadmin() context object and which not, but I have given up and mostly use restadmin for calls that have the purpose of getting user data...

     

    for your call, does it really need to be the email address as "unique identifier" for your users? Or could you also pass the user id (which would probably make things easier? You could also use the "login" (username)...and then use REST API v2 with something like

     

    <#assign response = restadmin("2.0", "SELECT avatar FROM users WHERE id = '<userid>'"?url) />
    <#if response.data?has_content>
       <#-- something like the following will give you the URL to the avatar image...-->
        ${response.data.avatar.message}
    </#if>
  • luk - the reason he is using the email is because they are pulling the data out side, and it is not necessary that the user id or user name would be same. User name could be but certainly not the int id.
  • luk's avatar
    luk
    Boss
    10 years ago

    Then your v1 API call with the email address in combination with (if necessary) restadmin() should do the trick...unfortunately v2 doesn't support the "email" field in WHERE conditions =/...

  • luk-Thank you, I really appreciate! Should I put the code that you provided in the previous post in the Endpoint "Text Content?" You mentioned about API v1. Can you provide steps on how to write a API code for this?  What I'm stuck at  is the Endpoint "Text Content" when it say, "enter the content to render in the endpoint."  API:smileyfrustrated:

     

    Thank you in advance....

  • luk's avatar
    luk
    Boss
    10 years ago

    I just tried in one of my communities, and it seems I'm able to retreive v1 API data via user/email calls for any user (but I guess you need to be logged in, that's why we use restadmin()):

     

    <#if http.request.parameters.name.get("email", "")?has_content>
    <#assign response = restadmin("/users/email/" + http.request.parameters.name.get("email")+ "/profiles/avatar") />
    <#if (response.image.url)?has_content>
    <#-- something like the following will print you the URL to the avatar image...-->
    ${response.image.url}
    </#if>
    </#if>

    This code you can put inside an endpoints "View Content" Field...To create an endpoint you go to Studio > Endpoints > "New Endpoint"(Button) > give it a name > paste code above > click "View <name> Endpoint" Link at the bottom of the field and add a urlencoded email address behind the endpoint address, like: 

     

    http://your.community.tld/communityid/plugins/custom/communityname/communityname/endpointname?email=user.email%40gmail.com

    this should then return you the URL to this specific users avatar (if everything goes well)