title: Converting a JS Rest API Call into Python
body: <p>I'm looking for assistance with translating a piece of sample code written in javascript into something that will work with Python. The sample code is for the 'POST Authenticate' Endpoint of CCH Axcess' Open Integration Kit, which sends a response containing a Security Token</p>
  <p>URL: <a href="https://api.cchaxcess.com/api/AuthServiceHelp/v1.0/help/operations/Authenticate" rel="nofollow noreferrer">https://api.cchaxcess.com/api/AuthServiceHelp/v1.0/help/operations/Authenticate</a></p>
  <p>Sample Code:</p>
  <pre><code>             var _restIntegratorKey = &quot;{User Integrator Key}&quot;;      // Please use this variable as global variable as all subsequent call required this integrator key to pass
               var _restHostUrl = &quot;{host url}&quot;; // i.e. &quot;{Protocol}://{host name}/api&quot;
               var request = $.ajax({
                   url: _restHostUrl + &quot;/AuthService/v1.0/Authenticate&quot;,
                   contentType: &quot;application/json; charset=utf-8&quot;, 
                   type: &quot;GET&quot;,
                   dataType: &quot;json&quot;
               });
               
               var request = {
                  UserName: &quot;{username}&quot;,      // Specify Username
                  UserSid: &quot;anyone&quot;,
                  Password: &quot;{password}&quot;,      // Specify Password
                  Realm: &quot;{accountnumber}&quot;     // Specify Account number
               }; 
          
               var request = $.ajax({
                  url: _restHostUrl + &quot;/AuthService/v1.0/Authenticate&quot;,
                  data: JSON.stringify(request),
                  
                  beforeSend: _setHttpHeader,
                  contentType: &quot;application/json&quot;,
                  type: &quot;POST&quot;,
                  dataType: &quot;json&quot;
               });
           
               request.done(function (data, textStatus, jqXHR) {
                   // This method block will be executed on succefull execution of api and will return security token.
                   // Below statement will store returned security token into browser cookie named: &quot;CchNGAuthToken&quot;. you can use any other way to persist this token.
                   // This security token we required to pass in header for other rest api calls
                   document.cookie = &quot;CchNGAuthToken=&quot; + data.Token;
               });
           
               request.fail(function (jqXHR, textStatus, err) {
                   // This method block will be executed if API returns any error.
               });
           
              // Please keep these below two methods, As both will be used in subsequent call, to get SSOToken from Cookie
              // set Security token and integratorkey into request header
              function _setHttpHeader(xhr) {
                  var ssoToken = _getSessionCookie(&quot;CchNGAuthToken&quot;);  // Read security token from document cookie which we set during authentication rest api call
                  xhr.setRequestHeader(&quot;Security&quot;, ssoToken);
                  xhr.setRequestHeader(&quot;IntegratorKey&quot;, _restIntegratorKey);
              };
             // Get session cookie 
             function _getSessionCookie(name) {
                 var key = name + &quot;=&quot;;
                 var cookie = &quot;&quot;;
             
                 if (document.cookie.length &gt; 0) {
                     var begin = document.cookie.indexOf(key)
                     if (begin != -1) {
                         begin += key.length
                         var end = document.cookie.indexOf(&quot;;&quot;, begin);
                          if (end == -1)
                             end = document.cookie.length;
                         cookie = unescape(document.cookie.substring(begin, end))
                     }
                 }
                 return cookie;
              };
           
  </code></pre>
  <p>I have an LIC file with the IntegratorKey stored in XML format, like:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot;?&gt;
  &lt;IntegratorInfo&gt;
      &lt;IntegratorKey&gt;XXXXX-XXX-XXX-XXXX&lt;/IntegratorKey&gt;
  &lt;/IntegratorInfo&gt;
  </code></pre>
  <p>I think my primary issue is identifying the values of the 'ssoToken' and '_restIntegratorKey' variables once they're assigned inside the '_setHttpHeader' function.</p>
  <p>My attempt:</p>
  <pre><code>def getcchauth():
       import requests
      
       integratorkey = getintkey()
       
  
       cookie = getcookie()
       
       authheaders = {
                'Security': cookie,
                'IntegratorKey': integratorkey,
                'content-type': 'application/json; charset=utf-8'        
                } 
       
       cchlogin = {
           'UserName':&quot;user&quot;,
           &quot;UserSid&quot;: 'anyone',
           'Password': 'password',
           'Realm' : '123456',
          }
  
       authattempt = requests.post(resthosturl+endpoint, data = cchlogin,
                                  headers=authheaders)
       
       print(authattempt.headers)
      
  def getcookie():
      import requests
      import json
      cookieresponse = requests.get(resthosturl+apiservdir,
      headers = headers)
      
      cookieresponse = json.dumps(cookieresponse.headers.__dict__['_store'])
      
      cookieresponse = json.loads(cookieresponse)
  
      
      cookie = 'CchNGAuthToken=' + cookieresponse['set-cookie'][1].split(';')[0].strip() + ';'
      print(cookie)
      
      return cookie
  </code></pre>
  <p>Thanks!</p>