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

@Community Alums 

property is of what type?

are you using email script in scoped app or global scope?

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

Community Alums
Not applicable

@Ankur Bawiskar 

Username property type: string

Amit136581_3-1699603767057.png

 

Password property type: password

Amit136581_4-1699603819252.png

 

Email script scope: global

Amit136581_2-1699603654396.png

 

@Community Alums 

try running this in background script and see it returns the correct password or not

if this runs fine then some other issue is present in your email script

var passwordEnc = new gs.getProperty('itamAPI_password');
var ge = new GlideEncrypter();
var password = ge.decrypt(passwordEnc);

gs.info(password);

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

@Ankur Bawiskar ,

 

Above script i checked in background script it is giving below output

 

Error occurred during decryption using KMFDecrypter: 
string may not be encrypted: Error occurred while trying to: SYMMETRIC_DECRYPTION
*** Script: [object GlideSystem]

@Community Alums 

I think you need to check this, seems something new in terms of feature

https://docs.servicenow.com/en-US/bundle/vancouver-platform-security/page/administer/key-management-framework/task/configure-script-encryption.html 

 

 

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