How to pass Basic Auth credentials to SOAP envelope

mballinger
Mega Guru

Hello,

 

I have a SOAP integration setup and all works well. My issue is, I am trying to pass Basic Auth credentials to the envelope. How can I do this? 

 

Also, I would like to use basic auth credentials via script when making this reference in a flow I have configured.

 

Thanks!

1 ACCEPTED SOLUTION


To pull the password from the basic auth profile instead of hardcoding it, you can use the setAuthenticationProfile method instead of setBasicAuth. Here's how you can do it:

- First, ensure that you have an authentication profile set up in ServiceNow. You can do this by navigating to System Web Services > Outbound > Basic Auth Profile.
- Create a new record and fill in the necessary details such as Name, Username, and Password.
- Once the profile is set up, you can use the setAuthenticationProfile method in your script. Here's an example:

javascript
try {
var s = new sn_ws.SOAPMessageV2('DAM', 'login');
// Use authentication profile
s.setAuthenticationProfile('basic', 'your_auth_profile_name'); // 'your_auth_profile_name' is the name of the basic auth profile you created
var response = s.execute();
var responseBody = response.getBody();
var status = response.getStatusCode();
} catch(ex) {
var message = ex.message;
}


In summary:

- Create a Basic Auth Profile in ServiceNow with the required credentials.
- Use the setAuthenticationProfile method in your script, passing 'basic' as the first argument and the name of your auth profile as the second argument.
- This method will automatically use the username and password from the specified auth profile, eliminating the need to hardcode these values in your script.


nowKB.com

For asking ServiceNow-related questions try this :
For a better and more optimistic result, please visit this website. It uses a Chat Generative Pre-Trained Transformer ( GPT ) technology for solving ServiceNow-related issues.
Link - https://nowgpt.ai/

For the ServiceNow Certified System Administrator exams try this :
https://www.udemy.com/course/servicenow-csa-admin-certification-exam-2023/?couponCode=NOW-DEVELOPER

View solution in original post

6 REPLIES 6

Rajdeep Ganguly
Mega Guru


Sure, you can pass Basic Auth credentials in SOAP integration in ServiceNow by using the setBasicAuth(userName, userPassword) method. Here's how you can do it:

1. Create a new SOAP Message function.
2. In the new SOAP Message function, you can set the Basic Auth credentials using the setBasicAuth method.

Here's a sample script:

javascript
var sm = new sn_ws.SOAPMessage('SOAP Message function', 'SOAP function');
sm.setBasicAuth('username', 'password');
var response = sm.execute();


To use Basic Auth credentials in a flow, you can use the REST step in Flow Designer and set the Authentication type to Basic. Here's how you can do it:

1. In Flow Designer, add a new action.
2. In the new action, add a REST step.
3. In the REST step, set the Authentication type to Basic.
4. Enter the username and password in the respective fields.

Here's a sample script:

javascript
(function execute(inputs, outputs) {
var r = new sn_ws.RESTMessageV2('REST Message function', 'REST function');
r.setBasicAuth('username', 'password');
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
})(inputs, outputs);


Please replace 'username' and 'password' with your actual credentials. Also, replace 'SOAP Message function' and 'SOAP function' with your actual SOAP Message function and SOAP function. Similarly, replace 'REST Message function' and 'REST function' with your actual REST Message function and REST function.


nowKB.com

For asking ServiceNow-related questions try this :
For a better and more optimistic result, please visit this website. It uses a Chat Generative Pre-Trained Transformer ( GPT ) technology for solving ServiceNow-related issues.
Link - https://nowgpt.ai/

For the ServiceNow Certified System Administrator exams try this :
https://www.udemy.com/course/servicenow-csa-admin-certification-exam-2023/?couponCode=NOW-DEVELOPER

Thank you for your response @Rajdeep Ganguly ! I know I can hardcode the username and password, but how can I pull the password in the script from the basic auth profile?

 

Here is my SOAP Message Function:

Screenshot 2024-02-29 at 9.15.52 AM.png

 Here is the script I am using:

try { 
 var s = new sn_ws.SOAPMessageV2('DAM', 'login');

//override authentication profile 
//authentication type ='basic'
s.setBasicAuth('username', 'password');

//set a MID server name if one wants to run the message on MID
//s.setMIDServer('MY_MID_SERVER');

//if the message is configured to communicate through ECC queue, either
//by setting a MID server or calling executeAsync, one needs to set skip_sensor
//to true. Otherwise, one may get an intermittent error that the response body is null
//s.setEccParameter('skip_sensor', true);

 var response = s.execute();
 var responseBody = response.getBody(); 
 var status = response.getStatusCode();
}
catch(ex) { 
 var message = ex.message;
}

 

I need to pass the username and password to the SOAP message identified in the first screenshot

 

Thanks!


To pull the password from the basic auth profile instead of hardcoding it, you can use the setAuthenticationProfile method instead of setBasicAuth. Here's how you can do it:

- First, ensure that you have an authentication profile set up in ServiceNow. You can do this by navigating to System Web Services > Outbound > Basic Auth Profile.
- Create a new record and fill in the necessary details such as Name, Username, and Password.
- Once the profile is set up, you can use the setAuthenticationProfile method in your script. Here's an example:

javascript
try {
var s = new sn_ws.SOAPMessageV2('DAM', 'login');
// Use authentication profile
s.setAuthenticationProfile('basic', 'your_auth_profile_name'); // 'your_auth_profile_name' is the name of the basic auth profile you created
var response = s.execute();
var responseBody = response.getBody();
var status = response.getStatusCode();
} catch(ex) {
var message = ex.message;
}


In summary:

- Create a Basic Auth Profile in ServiceNow with the required credentials.
- Use the setAuthenticationProfile method in your script, passing 'basic' as the first argument and the name of your auth profile as the second argument.
- This method will automatically use the username and password from the specified auth profile, eliminating the need to hardcode these values in your script.


nowKB.com

For asking ServiceNow-related questions try this :
For a better and more optimistic result, please visit this website. It uses a Chat Generative Pre-Trained Transformer ( GPT ) technology for solving ServiceNow-related issues.
Link - https://nowgpt.ai/

For the ServiceNow Certified System Administrator exams try this :
https://www.udemy.com/course/servicenow-csa-admin-certification-exam-2023/?couponCode=NOW-DEVELOPER

@Rajdeep Ganguly - My apologies, this is the script that I am currently using, but I want to pass the username and password to the SOAP message

try {
    var s = new sn_ws.SOAPMessageV2(c3Instance, 'login');

    s.setStringParameterNoEscape('Username', username);
    s.setStringParameterNoEscape('Password', password);

    var response = s.execute();
    var responseBody = response.getBody();
    var status = response.getStatusCode();

    var xmlDoc = new XMLDocument2();
    xmlDoc.parseXML(responseBody);
    var secToken = xmlDoc.getNodeText("//SecurityToken");

    workflow.scratchpad.sectoken = secToken;

} catch (ex) {
    var message = ex.message;
}