I tried to reproduce the error and the same occurs here. In my opinion your code is correct and I couldn't find a way to make it work.
But here are my two cents: are you sure you want to authenticate in the script itself? Isn't better if the user authenticate before calling your script service? This should be the regular flow of the request, unless you want to give guest access to some resources. In this case you can enable guest access to all of those resources.
My point is: I think you shouldn't try to authenticate using this script service, but the request itself should be already authenticated.