Method failed: (/api/now/table/incident) with code: 401 - Invalid username/password combo

venkata sai alu
Tera Contributor

Here I am implementing OAuth 2.0 for instance-to-instance integration (like Instance A and Instance B).

 

if I create one incident in Instance A then same incident should be created in Instance B with this OAuth 2.0 integration, I can be able to generate refresh token and access token but when I create incident in Instance A it is showing the below error. screenshot shared for your reference.

venkatasaialu_0-1737551215616.png

 

 

Below is the script include code, i will call this script include from scheduled job periodically and i will generate refresh token and i call this script from business rule to generate access token(business rule is triggered when incident is created).

 

var testDemoUtils = Class.create();
testDemoUtils.prototype = {
    initialize: function() {},

    type: 'testDemoUtils',
    generateRefreshToken: function() {
        gs.log('script includes called');
        var oauthcli = new sn_auth.GlideOAuthClient();
        var parameters = {
            grant_type: "password",
            username: "vinoth",
            password: "y?eQKtnA7vmnQ;EWoUM*jTN%-t-=Gy.6d$cY?QYmW_eaW1tyMs&8^R^Pg_xt;y;9TzkIKa43U}c+VN.8CG=,9$F2"
        };
       
        var jsonParams = new JSON();
        var tokenResp = oauthcli.requestToken('instanceToInstance_configuration', jsonParams.encode(parameters));
        var tokens = tokenResp.getToken();
       
        gs.setProperty('instanceToInstanceIng', tokens.getRefreshToken());
        gs.log("Refresh token "+tokens.getRefreshToken());
    },
    getAccessTokens: function(refreshToken) {
        gs.log("Refresh Token in access token si- "+refreshToken);
        //gs.log("getting Access token");
        var cliReq = new sn_auth.GlideOAuthClientRequest();
        cliReq.setGrantType('refresh_token');
        cliReq.setRefreshToken(refreshToken);
        cliReq.setScope(null);
       
        var oauthCli = new sn_auth.GlideOAuthClient();
        var tokResp = oauthCli.requestTokenByRequest('instanceToInstance_configuration', cliReq);
        var tokens = tokResp.getToken();
        return tokens.getAccessToken();
       

    },
    type: 'testDemoUtils'
};

 

Business rule script

 

(function executeRule(current, previous /*null when async*/ ) {

    try {
        var demoAccToken=new testDemoUtils().getAccessTokens(gs.getProperty('instanceToInstanceIng'));
        gs.log(demoAccToken+' access token in BR');
        var r = new sn_ws.RESTMessageV2('instanceToInstance_usecase''createIncident');
        r.setStringParameterNoEscape('access_token',demoAccToken);
        var response = r.execute();
        var responseBody = response.getBody();
        var httpStatus = response.getStatusCode();
        gs.addInfoMessage(responseBody);
        } catch (ex) {
        var message = ex.message;
    }

})(current, previous);
 
Rest message snaps
 
venkatasaialu_1-1737551215676.png

 

 

venkatasaialu_2-1737551215628.png

 

 

venkatasaialu_3-1737551215625.png

 

1 REPLY 1

Ankur Bawiskar
Tera Patron
Tera Patron

@venkata sai alu 

why to create duplicate questions?

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