The CreatorCon Call for Content is officially open! Get started here.

Need Assistant with Basic Athentication

Community Alums
Not applicable

Hi All, I have one email script to generate clickable link below code is a part of email script, Sometimes link gives an error ("414 Request - URI Too Large") error, As per checking URL length is too long approx 15k to 17k characters thats why that error came.

To fix this error I used below code, It is working fine, It converts Long url to tiny url by using POST method and create record in sys_tiny_url table and then used that tiny url in our email, It looks good and working as expected, but Hardcoding username / passwd in a script is not a good idea, Can anyone please suggest how can I proceed without hadrcoading of username and password in script

 

 

 

 //Maximum Practical length is 2086 charecters
    if (url.length < 2086) {

         gs.info('No need to convert in Tiny URL is');

        template.print('<a href=' + url + '>report link </a');

    } else {

        var request = new sn_ws.RESTMessageV2();
        request.setEndpoint('https://' + gs.getProperty('instance_name') + '.service-now.com/api/now/tinyurl');
        request.setHttpMethod('POST');

        //Eg. UserName="admin", Password="admin" for authentication, any basic servicenow user
        var user = "admin";
        var password = "admin";

        var body = {};
        body.url = 'https://' + gs.getProperty('instance_name') + '.service-now.com/itam_portal?id=my_it_asset_report&table=u_gap&filter=ccl_sys_class_name%3Dcmdb_ci_appl%5EORcl_sys_class_name%3Dcmdb_ci_computer%5Ecl_install_statusNOT%20IN111,14%5Ecl_u_number%20IN' + hwname + '%5Ecl_assignment_group%2Emanager=' + manager + '&view=my_it_asset_report'; //url that need to be shortened


        request.setBasicAuth(user, password);

        request.setRequestHeader("Accept", "application/json");

        request.setRequestHeader('Content-Type', 'application/json');

        request.setRequestBody(JSON.stringify(body));

        var response = request.execute();

        var jsonString = response.getBody().toString(); // get JSON of url

        var cleanedString = jsonString.replace(/["}]+$/g, '');

        var startPos = cleanedString.indexOf('"result":"');
        if (startPos !== -1) {

            var shorturl = cleanedString.substring(startPos + '"result":"'.length);

            
        }
         gs.info('Tiny URL is: ' + shorturl);

        template.print('<a href=' + shorturl + '>report link </a');

    }

 

 

1 ACCEPTED SOLUTION

@Community Alums 

setup the profile in REST Message then inside your Rest message function set the authentication type as Inherit from parent or you can again select the same basic auth profile

add the profile name which you create as 2nd parameter

//Maximum Practical length is 2086 charecters
if (url.length < 2086) {

gs.info('No need to convert in Tiny URL is');

template.print('<a href=' + url + '>report link </a');

} else {

var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://' + gs.getProperty('instance_name') + '.service-now.com/api/now/tinyurl');
request.setHttpMethod('POST');

//Eg. UserName="admin", Password="admin" for authentication, any basic servicenow user
// var user = "admin"; // not required
//var password = "admin"; // not required

var body = {};
body.url = 'https://' + gs.getProperty('instance_name') + '.service-now.com/itam_portal?id=my_it_asset_report&table=u_gap&filter=ccl_sys_class_name%3Dcmdb_ci_appl%5EORcl_sys_class_name%3Dcmdb_ci_computer%5Ecl_install_statusNOT%20IN111,14%5Ecl_u_number%20IN' + hwname + '%5Ecl_assignment_group%2Emanager=' + manager + '&view=my_it_asset_report'; //url that need to be shortened


//request.setBasicAuth(user, password); // this is not required

request.setAuthenticationProfile('basic', profile name); // give the basic auth profile name here

request.setRequestHeader("Accept", "application/json");

request.setRequestHeader('Content-Type', 'application/json');

request.setRequestBody(JSON.stringify(body));

var response = request.execute();

var jsonString = response.getBody().toString(); // get JSON of url

var cleanedString = jsonString.replace(/["}]+$/g, '');

var startPos = cleanedString.indexOf('"result":"');
if (startPos !== -1) {

var shorturl = cleanedString.substring(startPos + '"result":"'.length);


}
gs.info('Tiny URL is: ' + shorturl);

template.print('<a href=' + shorturl + '>report link </a');

}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

17 REPLIES 17

Ankur Bawiskar
Tera Patron
Tera Patron

@Community Alums 

store the user-name and password in system property.

for storing password use password type property and then use gs.getProperty() to fetch those 2 values

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Community Alums
Not applicable

Hi @Ankur Bawiskar ,

 

Thanks for your prompt reply!

 

How I store user-name and password in system property and how to use that in email script.

@Community Alums 

it is simple

1) in left navigation type sys_properties.list
2) create new record and give property name and the type for username give type as string, for storing password give type as password

3) then use gs.getProperty() in email script

 

AnkurBawiskar_0-1699601551906.png

 

AnkurBawiskar_1-1699601580423.png

the password is encrypted so you need to decrypt it

Below are the changes in bold which you need

//Maximum Practical length is 2086 charecters
if (url.length < 2086) {

gs.info('No need to convert in Tiny URL is');

template.print('<a href=' + url + '>report link </a');

} else {

var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://' + gs.getProperty('instance_name') + '.service-now.com/api/now/tinyurl');
request.setHttpMethod('POST');

//Eg. UserName="admin", Password="admin" for authentication, any basic servicenow user
var user = gs.getProperty('itamAPI_userName');
var passwordEnc = new gs.getProperty('itamAPI_password');
var ge = new GlideEncrypter();
var password = ge.decrypt(passwordEnc);

var body = {};
body.url = 'https://' + gs.getProperty('instance_name') + '.service-now.com/itam_portal?id=my_it_asset_report&table=u_gap&filter=ccl_sys_class_name%3Dcmdb_ci_appl%5EORcl_sys_class_name%3Dcmdb_ci_computer%5Ecl_install_statusNOT%20IN111,14%5Ecl_u_number%20IN' + hwname + '%5Ecl_assignment_group%2Emanager=' + manager + '&view=my_it_asset_report'; //url that need to be shortened


request.setBasicAuth(user, password);

request.setRequestHeader("Accept", "application/json");

request.setRequestHeader('Content-Type', 'application/json');

request.setRequestBody(JSON.stringify(body));

var response = request.execute();

var jsonString = response.getBody().toString(); // get JSON of url

var cleanedString = jsonString.replace(/["}]+$/g, '');

var startPos = cleanedString.indexOf('"result":"');
if (startPos !== -1) {

var shorturl = cleanedString.substring(startPos + '"result":"'.length);


}
gs.info('Tiny URL is: ' + shorturl);

template.print('<a href=' + shorturl + '>report link </a');

}

If my response helped please mark it correct and close the thread so that it benefits future readers.

 

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Community Alums
Not applicable

Hi @Ankur Bawiskar ,

 I tried above method, got below error

 

Error occurred during decryption using KMFDecrypter: 
string may not be encrypted: Error occurred while trying to: SYMMETRIC_DECRYPTION 

How can I resolve this?