You are viewing limited content. For full access, please sign in.

Question

Question

Getting Access Token from Run Script Rule in Cloud

asked on October 3, 2022

can anyone tell me why this function does not work when run with the Run Script Rule in cloud? 

AccessKey accessKey = AccessKey.CreateFromBase64EncodedAccessKey(base64EncodedAccesskey);

 

If I hardcode the token the rest of the code works as expected but I cannot seem to get a valid token from this method to use the API

 

Any thoughts on how to fix the issue will be appreciated, thanks. 

0 0

Replies

replied on October 4, 2022

Hi,
Would you please provide more information about what is going wrong? Is there any error message or exception information available to help looking into the problem?

0 0
replied on October 4, 2022

There are no error messages available to my knowledge. when running the script using the "test script" button in the cloud rules, it states that it runs successfully with no errors but the api token is not generated to make the api calls. When I try to return the apiToken variable it is blank. Returning an error message is also blank. 

 

here is my code for this function: 

static string GetApiToken(string servicePrincipalKey, string base64EncodedAccesskey)
        {
            Console.WriteLine("***Getting Token***\n");
            var apiToken = "";
            
            
            try
            {
                AccessKey accessKey = AccessKey.CreateFromBase64EncodedAccessKey(base64EncodedAccesskey);//error
                TokenClient tokenClient = new TokenClient(accessKey.Domain);
                var response = tokenClient.GetAccessTokenFromServicePrincipalAsync(servicePrincipalKey, accessKey);
                apiToken = response.Result.Access_token;
                Console.WriteLine("apitoken for swagger = " + response.Result.Access_token + "\n");
            }
            catch (ApiException<ProblemDetails> ex)
            {
                Console.WriteLine(ex.Result.Title);
                Console.WriteLine(JsonConvert.SerializeObject(ex.Result));
                return ex.Message.ToString();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return ex.ToString();
            }
            
            return apiToken;
        }
 

Please let me know if there is any other information I can provide. 

0 0
replied on October 5, 2022

Hi Brandon, is it possible to print out the exception directly?


I am asking because you said there's no error message and I read your code and it seems to me, if ApiException happens, and if for some reason the ex.Result.Title and ex.Result is null, you won't get any error message.

 

Similarly, when catching Exception, if ex.Message is empty, you would nothing in the standard output as well.

 

Also, you mentioned getting an empty string for access token. Based on your code, it is possible that it's either one of two catch clause that returned. In that case, if ex.Message is empty or ex.ToString() returns an empty string, you will get an empty string.

0 0
replied on October 5, 2022

the same code runs without issue in a separate console application, including the line mentioned above initially. 

The same function would not work when switched to a class library run from the cloud. 

As for getting an error message the only way to get an error message that I can see is returning one back to the script in the cloud. unless someone has a better idea? 

Even though the code is in a try catch block, if I try to return something along with the error message it will not do it signaling that the program does not complete to return anything at all and just terminates. 

 

I have switched the code into the initial function and instead of being called and in the cloud i get this generic error: 

 

0 0
replied on October 6, 2022

Hi

The dotnet client libraries are open source, so if it helps, the code for that function can be found here

AccessKey accessKey = AccessKey.CreateFromBase64EncodedAccessKey(base64EncodedAccesskey);

https://github.com/Laserfiche/lf-api-client-core-dotnet/blob/33adfe325a4d63f1c673408f8b30656eb07bf51e/src/OAuth/AccessKey.cs#L26

 

To figure out why the exception is being thrown, are you able to see what the Console is printing? And are you able to see what error messages are being returned? If you are able to return the error message to the script in the cloud, then that would probably help to figure out what is going wrong.

 

Additionally, are all dependency files (dll) like the Laserfiche.Api.Client.Core able to be loaded by the script? 

0 0
replied on October 6, 2022

as far as I can tell all dependency files can be loaded unless I am missing a way to assess this? 

 

I cannot return any value at all. When I put the function in question to the original method : 

I will get the red error message from above :

When I call a function that then calls this function I get this message: 

and in both cases I cannot return a value to assess:

this above will return nothing. no string at the end. 

0 0
replied on October 7, 2022 Show version history

Hi, to get more details for the red error message, could you check the windows event viewer? You should be able to find events for your remote agent under

Applications and Service Logs -> Laserfiche -> ProcessAutomation -> AgentFolder

0 0
replied on October 7, 2022

Thank you I think this is the error to solve: 

How can this be remedied ? 

0 0
replied on October 7, 2022

Perhaps the remote agent does not have access to the dlls used by the class library. Could you check steps 4 and 5 in this documentation

https://doc.laserfiche.com/laserfiche.documentation/en-us/Subsystems/ProcessAutomation/Content/Resources/Rules/scripts.htm

Check if the dlls used by the class library can be find in the folders specified in the AllowList in "plugin_settings.json" in the remote agent's folder. 

0 0
replied on October 7, 2022

adding all dll files into the proper folder got rid of the red error message but now there is a different error message that I can return from a catch block: 

Could not load file or assembly 'Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

0 0
replied on October 11, 2022

Hi, can you check if the "plugin_settings.json" file for the remote agent includes the default "." in the AllowList? I think this is the remote agent's bin folder which already contains newtonsoft 12, while the Laserfiche.Api.Client.Core uses newtonsoft 13. Could you try to remove "." from the AllowList and use a different folder that only includes the dlls for the script?

0 0
replied on October 11, 2022

I moved the dll files for the script to a different location and specified it in "plugin_settings.json" which produces the same error. 

0 0
replied on October 12, 2022

Hi, could you try using the latest version of Laserfiche.Api.Client.Core (version 1.2.3) and see if you still see this error?

0 0
replied on October 13, 2022

I still get the same error unfortunately 

0 0
replied on October 13, 2022

Hi, just making sure you've cleaned and built the project after using Laserfiche.Api.Client.Core version 1.2.3 and the built dlls are now in the folder specified in the AllowList.

One change we've made to Laserfiche.Api.Client.Core version 1.2.3 is lower the Newtonsoft.Json version being used to version 12, so if you are still seeing an error, hopefully it is different from the previous.

Additionally, if you continue seeing errors, then it might be easier to troubleshoot this through a support case so we can take a closer look at your configuration. Please open a case, attach your sample code and reference this thread

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.