Make a REST api call which has authentication for a button click

Sravya Reddyset
Tera Contributor

Hi everyone, 

 

We have a requirement of fetching the response from by calling the API which requires authentication.

From the UI, when a user clicks on a button, we would like to make this API call and populate the response the UI form text.

 

Tried options

1. API call directly through UI action - We were able to call the APIs which were open but with authentication its throwing 511 error (Network Authentication Required)

2. API call from UI Action via Rest Message -  Faced null pointer exception

3. Script Includes internally uses GlideAjax - cross scope access privilege error

 

Seems like we are struck with CORS error, some rules have to be added to address the same.

 

Could anyone help us if the above mentioned task can be achieved in ServiceNow. Looking forward for your assistance.

4 REPLIES 4

Amit Verma
Kilo Patron
Kilo Patron

Hi @Sravya Reddyset 

 

Below post could be helpful to configure the UI Action :

https://www.servicenow.com/community/itsm-forum/call-rest-api-through-script-include-and-display-thr...

 

For the authentication issue, did you tested the API call from the REST message you created ? If your authentication works over there, I don't think you should run into issues as the authentication is being handled and configured in REST message itself, not in the UI Action or Script Include.

 

Thanks and Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

Hi @Amit Verma,

 

Yes, our REST Message was working fine without any errors, and  based on your inputs we have used script includes.

 

However, we faced cross -scoped access privileges,  we have added the relevant records for the target and source scopes, could you confirm if that is the right way or should we change scope of our script includes, if so, how do we ship it to the required scoped application.

 

Thanks in advance , looking forward for your response.

 

Refer the below error message.

 

Error MessageExecute operation on API 'ScriptableRESTMessageClient.setHttpMethod' from scope 'our application scope name' was denied. The application 'our application scope name' must declare a cross scope access privilege. Please contact the application author to update their privilege requests in servicenow while executing above code via UI action

 

 

Regards,

Sravya

 

Hi @Sravya Reddyset 

 

Script Includes could be part of your application scope itself. This should remediate scope related issues. You can switch to your application scope and create copy of your existing script include. This will capture it under your application scope.


Please mark this response as correct and helpful if it assisted you with your question.

Hi @Amit Verma ,

 

I understand, however, below records are what we have created in the cross scoped privileges.

Is that a right way ?

 

Source Scope: Our application name

Application : Our application name
Target Scope: Global
Target Name: ScriptableRESTMessageClient.setHttpMethod
Target Type: Scriptable
Operation: Execute API

 

Since ScriptableRESTMessageClient.setHttpMethod was not working in our application scope.