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

Ankur Bawiskar
Tera Patron
Tera Patron

@Priyanka_77 

so you are consuming 3rd party API with OAuth details and trying to get token

You can use script to obtain the refresh and access token from script, script will vary based on grant type somewhat

refer below links

OAuth2.0: Get new Access Token from existing Refresh Token

How to Setup OAuth2 authentication for outbound RESTMessageV2 integrations

OAuth : Script to Automate Token Request

How to generate the access token once it is expired from Business Rule?

How to get Auth Token using script when grant type is Authorization code

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

Hi,
I am not using oauth 2.0..I am using no authentication.Please tell me for above sceanrio using sys_properties.

@Priyanka_77 

then better to check with 3rd party on how to consume their endpoint.

They should have some documentation on how to do

how are you doing it in Postman?

Do similar thing in ServiceNow

Please share postman screenshot where it worked

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

We are directly hitting the endpoint using no authentication and passing the clientid,client secret and grant ype in the content.
But the requirement is the endpoint, client id and client secret needs to be stored in a system property.
I have checked the configurations, code, still it is giving me error 401.