3 Replies Latest reply on Feb 2, 2016 6:44 AM by Ryan Rutan

    Authentication using JavaScript, Python, and the Rest API

    thales

      I have been able to use the Rest API through Python without any problems. The authentication uses Basic Authentication similar to this example:

       

      #Define headers
      user = "username" #User must have social group permissions
      password = "password"
      
      auth = "Basic " + base64.encodestring('%s:%s' % (user, password)).replace("\n","");
      headers = { "Content-Type": "application/json", "Authorization": auth }
      
      #Send get request
      req = requests.get(url, headers=headers )
      data = req.content
      

       

      I would like to create a user friendly interface for performing basic community management functions. I figured using HTML, JavaScript, and the Rest API would be the simplest solution; however, I am getting an access error:

      No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8383' is therefore not allowed access. (07:00:39:215 | error, javascript)

        at public_html/index.html

      Here is my JavaScript code:

      //Define headers
      var user = "username"; //User must have social group permissions
      var password = "password";
      var credentials = window.btoa(user+":"+password);
      
      var auth = "Basic "+credentials; //.format(credentials);
      
      //Send get request
      var req =  new XMLHttpRequest();
      req.open("GET",url);
      req.setRequestHeader("Content-Type", "application/json");
      req.setRequestHeader("Authorization", auth);
      req.send();
      var data = req.responseText;
      

       

      Based upon the following threads, it appears that it is not possible to pass a Rest API call from a local server to our cloud based community. I need to put the HTML/JavaScript on the same server as my community.

       

      Using the V3 REST API from other applications/servers

      Got “Access to restricted URI denied” error after sent request with URL and credential

       

      This creates some confusion for me. Why does the Rest API work from Python but not JavaScript? Is there a better way to create a simple interface for using the Rest API? Should I just rely on Python and some interface module?