Thank for that hint. Seems my original problem was not one of authority after all. The client program my script is calling had some sort of internal API error which I'd misinterpreted as authority. My misunderstanding of the impersonation added to my problem.
Mysteriously the API problem 'went away by itself' the other night, so my script is now working. I'm stumped as to what the original problem was though.
Thanks for your help.