Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

oAuth2 Script To Pull Back Token

fcaruso123
Tera Expert

I've created a REST Message using an oAuth profile. Using the Get OAuth Token link I am able to successfully pull back a token. Am now trying to script the same call in a business rule. I've read many articles and tried several variations but am not able to pull back a token. The oAuth profile uses a Grant Type of "Client Credentials. I'm in Global scope. At this point I am confused about the difference between GlideOAuthClientRequest() and GlideOAuthClient, which to use and what parameters to pass. Example scripts talk about oauth_profile_id and oath_entity_id but it s not clear if those are sys_id values or something else. Currently using the following:

           var oauth_profile_id = 'c4600d06ebb0821023a9fd59bad0cd43';
            var params = {grant_type:"Client Credentials",oauth_provider_profile:oauth_profile_id};
            var json = new global.JSON();
            var text = json.encode(params);

            var oAuthClient = new sn_auth.GlideOAuthClient();

            try {
            	var tokenResponse = oAuthClient.requestToken('Test');
            	var token = tokenResponse.getToken();
            	var access_token = token.getAccessToken();
            } catch(ex) {
            	alert ('asasasas');
            }

Any advice is appreciated.

 

1 ACCEPTED SOLUTION

fcaruso123
Tera Expert

I found my mistake. I was using "Client Credentials" instead of "client_credentials". New code works to obtain token and make API call:

    var params = {
        grant_type: "client_credentials",
        oauth_provider_profile: "c4600d06ebb0821023a9fd59bad0cd43"
    };
    var json = new global.JSON();
    var text = json.encode(params);
    var oAuthClient = new GlideOAuthClient();

    try {
        var tokenResponse = oAuthClient.requestToken('Test', text);
        var token = tokenResponse.getToken();
        var access_token = token.getAccessToken();

	var r = new sn_ws.RESTMessageV2('Action', 'Create Ticket');
	r.setRequestHeader('Authorization',access_token);
	r.setRequestHeader('Content-Type','application/json');
	var response = r.execute();
	current.u_string_1 = response.getBody();
	current.u_string_1 += '\n' + token.getExpiresIn();
    } catch (ex) {
        alert('asasasas');
    }

View solution in original post

1 REPLY 1

fcaruso123
Tera Expert

I found my mistake. I was using "Client Credentials" instead of "client_credentials". New code works to obtain token and make API call:

    var params = {
        grant_type: "client_credentials",
        oauth_provider_profile: "c4600d06ebb0821023a9fd59bad0cd43"
    };
    var json = new global.JSON();
    var text = json.encode(params);
    var oAuthClient = new GlideOAuthClient();

    try {
        var tokenResponse = oAuthClient.requestToken('Test', text);
        var token = tokenResponse.getToken();
        var access_token = token.getAccessToken();

	var r = new sn_ws.RESTMessageV2('Action', 'Create Ticket');
	r.setRequestHeader('Authorization',access_token);
	r.setRequestHeader('Content-Type','application/json');
	var response = r.execute();
	current.u_string_1 = response.getBody();
	current.u_string_1 += '\n' + token.getExpiresIn();
    } catch (ex) {
        alert('asasasas');
    }