Forum Discussion

Re: RESTful API Header

Thanks for looking at this when you have time. I just defined two separate constants xmlHttp1 & xmlHttp2. The issue is trying to use the ticket outside of the function. I added a check before trying to pass to the next script block and the ticket is undefined outside of the function; the query executes, but does not have authorization:
<!---------------GET_TEMPORARY_TOKEN---------------------->

<script>
var ticket;
var tempAuth;
var headers = {
  	'QB-Realm-Hostname': '[MY REALM]',
	'QB-App-Token': '[MY APP TOKEN]',
    'Content-Type': 'application/json'
}


const xmlHttp1 = new XMLHttpRequest();
xmlHttp1.open('GET', 'https://api.quickbase.com/v1/auth/temporary/[MY TABLE ID]', true);
for (const key in headers) {
  xmlHttp1.setRequestHeader(key, headers[key]);
}
xmlHttp1.onreadystatechange = function() {
  if (xmlHttp1.readyState === XMLHttpRequest.DONE) {
    console.log(xmlHttp1.responseText);
document.getElementById("AUTH#").innerHTML = xmlHttp1.responseText;
ticket = JSON.parse(xmlHttp1.responseText);
document.getElementById("VAR#").innerHTML = ticket.temporaryAuthorization;
  }
};
xmlHttp1.withCredentials = true;
xmlHttp1.send();

ticket1 = xmlHttp1.onreadystatechange("xmlHttp1.responseText");
console.log(xmlHttp1.responseText);
tempAuth = "QB-TEMP-TOKEN " + ticket1;
document.getElementById("CHK#").innerHTML = tempAuth;

</script>​



------------------------------
Adam Keever
------------------------------

15 Replies

  • AustinK's avatar
    AustinK
    Qrew Commander
    I thought this was a scope issue and it may have been at first before some changes I made but this appears to be an issue with the asynchronous calls here. I believe what is happening is after the first xmlHttp request happens and is returned it has already fired the second one as well, so the tempAuth is actually undefined at that time but it gets defined not long after that.

    The way you can tell this is happening is to load the code page(or however you call this) and then after it loads and errors hit F12 - in chrome - and manually type tempAuth, you'll see that it is indeed correct with the token there and everything.

    You will need to nest the xmlHttp calls together or utilize promises so that things are done in a certain order and not all at once.
    • BradLemke's avatar
      BradLemke
      Qrew Assistant Captain
      I've been following this thread and bashing my face against this brick wall all day.  I've been seeing something similar to you Austin.

      So, just for kicks, I changed 
      xmlHttp1.open('GET', 'https://api.quickbase.com/v1/auth/temporary/[MY TABLEID]', true false);

      This made it run in synchronous mode, and now my query went straight through like it was supposed to when I set the query's header to 'Authorization': 'QB-TEMP-TOKEN ' + ticket.temporaryAuthorization,

      ------------------------------
      Brad Lemke
      ------------------------------
      • AustinK's avatar
        AustinK
        Qrew Commander
        This is my experience in general when trying to slap together a bunch of code examples, it is so hard to debug. This was driving me crazy too because it seemed like it should be working. That's why I had to actually try testing it today to see what was going on.

        I should have looked at the xmlHttp command closer. Switching that to false does make that into a synchronous call which should make it all work. From everything I see online this is technically bad practice because it can cause a wait for the user but in this case I don't think it would really matter. I bet it never even causes a problem in this case, there isn;t anything major going on and I don't expect Quickbase to not answer an api call.

        Thanks for posting that Brad.