How to Fetch and Use OAuth2.0 Tokens from an OAuth provider in ServiceNow
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-08-2022 09:43 PM - edited 05-15-2025 06:20 AM
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:
** 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.
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.
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());
- Labels:
-
flow designer
-
Workflow Automation
- 11,045 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2024 01:06 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2025 06:21 AM
Thanks Ollop, I updated the code now 🙂