i created a oauth profile to connect to a third party oauth provider - invalid client credentials

Priyanka_77
Tera Contributor

Hi,

 

I have created an POST method to get the token from the server.Prviously, i have used No Authentication, with the client id client secret and grant_type passed in the Content of Outbound Rest Message.
Now, I want to use system property to store the endpoint, clientid, client secret and grant type in a sys_property and use that in the script include.

I tried to create an oauth profile, but it is giving me Invalid client credentials but i tried to test it using postman, it is working.
So, tell me how can i use No authentication and store the details in sys_property.
Below is the implementation done by me,
As i said I have one outbound rest message which has the endpoint and inside that i have created one POST message with the endpoint stored in ${x_abc.token.endpoint} and then in the same post method i have HTTP parameters and in the content i have :
client_id =${x_abc.icu_token_client_id}&client_secret=${x_abc.icu_token_client_secret}&grant_type=client_credentials

So, for this i have created 3 system properties in sys_properties table and in the UI action, i have called them sing below is the function to get the access token,

getAccessToken: function() {
        try {
             var clientId = gs.getProperty('x_abc.icu_token_client_id');
            var clientSecret = gs.getProperty('x_abc.icu_token_client_secret');
            var tokenEndpoint = gs.getProperty('x_abc.token.endpoint');

            var oRequest = new sn_ws.RESTMessageV2('x_abc.Token Generation', 'Generate Token');
            // oRequest.setStringParameterNoEscape('x_abc.icu_token_client_secret', clientSecret);
            // oRequest.setStringParameterNoEscape('x_abc.icu_token_client_id', clientId);
            oRequest.setStringParameterNoEscape('x_abc.icu_token_client_secret', clientSecret);
            oRequest.setStringParameterNoEscape('x_abc.icu_token_client_id', clientId);
            oRequest.setStringParameterNoEscape('x_abc.token.endpoint', tokenEndpoint);


            //oRequest.setEndpoint(tokenEndpoint);


            var oResponse = oRequest.execute();
            var status = String(oResponse.getStatusCode());

            if (status.startsWith('20')) {
                var responseBody = oResponse.getBody();
                var response = JSON.parse(responseBody);
                return response.access_token; // Return the access token
            } else {
                //gs.error('Failed to get access token. Status code: ' + status);
                return null;
            }
        } catch (error) {
            //gs.info("Error during token request: " + error.message);
            return null;
        }
    },
 
i checked the configurations, it is correct.

 

 

7 REPLIES 7

@Priyanka_77 

Please share screenshots how are you hitting it via postman?

do something similar from ServiceNow as well.

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

Muhammad Salar
Giga Sage

Hello, may be it is the issue with sys_property. Check this thread,
Solved: Re: Issue: Dynamic Script condition in SLA filter ... - Page 3 - ServiceNow Community
As mentioned by @Ankur Bawiskar 

gs.getProperty() has some issues when called from client callable script include

use GlideRecord to query sys_properties table with your name of property and then return the value

Script include is not client callable.