I do not have time to fully test this right now but I will later if this does not make it work for you. Those errors are because you try and initialize that variable multiple times.
You set it as a const on line 62 and then also set it as const on line 99. I believe if you remove the const on line 99 it should work.
There is a second part to this though. When you set a variable as const you are saying it should not be allowed to be changed, kind of(it gets complicated.) So when you set it initially at line 62 that is fine. When you try and re-assign the variable later on it is blocked because it is a const.
If you set the first variable to be var instead of const (and then remove the const from line 99 still) that may work. Like I said I will try and actually test this tomorrow or possibly later today when I get a chance.
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:
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.