Hi Smitha -
Generally, trusted tickets are "restricted" in terms of only allowing the user to show a visualization. If they are used to try and browse a list of reports any the Tableau Portal's chrome, you get thrown to the login screen. It sounds like this is what is happening to you.
The scriptlet that you pasted in above is just the standard stuff that Tableau generates - I'm not clear on how you're actually trying to dynamically call it as your question indicates you are doing. Can you elaborate?
If worse comes to worse, I suspect enabling "unrestricted tickets" will solve this problem for you (you can search on those keywords in Google for more info) but I wouldn't do that except as a last resort.