How to Fetch and Use OAuth2.0 Tokens from an OAuth provider in ServiceNow

Aditya Kumar6
Tera Contributor

How to Fetch and Use OAuth2.0 Tokens from an OAuth provider in ServiceNow

 

Part1: Create an OAuth Provider entity:

 

Step1: Navigate to System oAuth > Application Registry > Connect to an OAuth Provider (Simplified)

Step2: Provide Name(Any) ,  Client ID (Provided by the web service), Client Secret (Provided by the web service), Token URL (Provided by the web service) and Entity Scope (If any) in the form as show below:

Aditya25_0-1667972275930.png

 

** You can also provide Authorization URL if it is a part of the token generation process as defined by  the provider.

Step3: Click Save and Stay on the form.

Step4: A Default OAuth Entity profile will be created for your registry in the form. Click on the Profile created.

Aditya25_1-1667972275937.png

 

Step5: In the OAuth Entity Profile you can add Entity scopes as well if any provided by the OAuth credential provider. You can also change the name of this profile if you wish to do so. 

Aditya25_2-1667972275941.png

 

 

 

Part2: Create OAuth2.0 Credential entry for the token

 

 

Step1: Integration hub -> Connections & credentials -> credentials

Step2: Click New and create a OAuth2.0 credential.

Step3: Enter Name and add the OAuth Entity Profile we created in Part1.

Step4: Click save and stay on the form.

Step5: Click on Get OAuth Token to fetch the token from the provider.

A new token will be fetched at this step. Although this token is not visible to end user you can fetch this token in a flow/workflow or a script using the following script.

 

var oAuthClient = new sn_auth.GlideOAuthClient();

                  var requestor_context = 'test';

                  var requestor_id = 'provide requestor id here';

                  var oauth_profile_id = 'Sys Id of the Entity profile'; // profile ID [sys_id of  'OAuth Entity Profiles' (oauth_entity_profile) record in OAUTH registry  record]

 

                  var params = {grant_type:"client_credentials", oauth_requestor_context:requestor_context, oauth_requestor:requestor_id, oauth_provider_profile:oauth_profile_id}; //

                  var json = new global.JSON();

                  var text = json.encode(params);

                  var tokenResponse = oAuthClient.requestToken('oAuth Test', text); //'oAuth Test' is the name of the OAuth application registry record (oauth_entity)

                  var token = tokenResponse.getToken();

                  var access_token = token.getAccessToken() ;

                  gs.log("AccessToken:" + access_token);

                  gs.log("AccessTokenExpiresIn:" + token.getExpiresIn());

                  gs.log(" RefreshToken:" + token.getRefreshToken());

2 REPLIES 2

ollop
Tera Contributor

The code contains an error: I suppose that the row "var tokenResponse = oAuthClient.requestToken(oAuth Test', text);" should be have another quote (') after the bracket.... I mean
var tokenResponse = oAuthClient.requestToken('oAuth Test', text);

 

Thanks Ollop,  I updated the code now 🙂