Forum Discussion

iftomkins's avatar
11 years ago

Sample REST API Call from external site

Here is a sample REST API call using jQuery. This is for accessing the API from an external site (not your community site). It took me a long time to get the syntax right (with the help of Lithium Customer Support), so I wanted to share it here.

 

//Authenticate 

$.post("https://yourcommunity.com/yourcommunityid/restapi/vc/authentication/sessions/login", { "user.login": 'username', "user.password": 'password'}, function(document,err,responseData) { 

xml = responseData.responseText; 

xmlDoc = $.parseXML( xml ); 

xml = $( xmlDoc ); 

sessionKey = xml.find( "value" ).text(); 

}); 

 

//Get Info 

$.post('https://yourcommunity.com/yourcommunityid/restapi/vc/boards/id/force/threads/latest', { "restapi.session_key": sessionKey}, function(document,err,responseData){ 

xml = responseData.responseText; 

xmlDoc = $.parseXML( xml ), 

xml = $( xmlDoc ), 

xml.find("thread").each(function(){ 

var thread_title = $(this).find("title").text(); 

$("ul.featured_topics").append("<li>" + thread_title + "</li>") 

}); 

}); 

 

Note: To make rest api calls from external domains, you must ask Lithium support to add them to a safe list.

  • AdamN's avatar
    AdamN
    Khoros Oracle

    Hi iftomkins,

     

    Thanks for sharing!

     

    Out of curiosity, are you having users pass through their own username and password? Or are you using some kind of common dedicated rest user account? I just want to caution anyone that might use this approach against storing credentials in client-side code, to prevent accounts from being compromised.

    • iftomkins's avatar
      iftomkins
      Maven
      It?s a dedicated REST API user account. If there is a more secure way to do it, please let me know. Thanks!
      • Gursimrat's avatar
        Gursimrat
        Leader

        You mean accessing community data on other sites using the REST APIs of community?

  • I wanted to update this thread to say that I can no longer make this REST API call work. Just in case someone is having issues with it, I didn't want you to think it was working on our instance. 

     

    It could be an issue with the whitelisting of the domains, as indicated by this error: http://screencast.com/t/HTnFyl0iCeUH.