Insight IQLFacade scripts works in Insight Script Console, but not work in Exalate script

Hi everyone,

I’m searching Insight objects using IQL, the code as the following:

Class iqlFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.IQLFacade")
def iqlFacade = ComponentAccessor.getOSGiComponentInstanceOfType(iqlFacadeClass)

def insightObjectName = "My Object"
def objects = iqlFacade.findObjectsByIQLAndSchema(2, "Name = \"" + insightObjectName + "\"")

This code works if I run it from Insight Script Console, and it returns a list with My Object inside.
But when I paste it to the Exalate incoming script, it does not work anymore.

So what is the problem here?
Thank you very much!

STATUS UPDATED

I tried to do a loadObjectBean from both sides.
The object is loaded successfully from Insight Script Console.
On the Exalate side, it threw an Exception PermissionInsightException: Anonymous User., so I think the problem here related to Permission.
So does anyone know how to fix this?

def object = objectFacade.loadObjectBean("IFI-116")

As always, I want to save the solution as a reference for the future.

I fixed it by set current logged in user to a user who has permission to browser Insight objects:

def authContext = ComponentAccessor.getJiraAuthenticationContext();
def currentUser = ComponentAccessor.getUserManager().getUser("admin")
authContext.setLoggedInUser(currentUser);