Open URL in background (scheduled script or script include)

Kamil Smusz
Kilo Sage

Hello experts,

 

I working on automation for OAuth authorization and i need to open URL from server site. Next this URL will redirect to new one and return parameter in URL link. Is it possible?

 

so steps should look like below:
1. open url in background
2. get parameter from redirected url

3. use parameter value in script

 

9 REPLIES 9

Mike_R
Kilo Patron
Kilo Patron

Is this for Oauth? If so, this isn't the best or more secure way.

Hi Mike, 

I’m aware that this is not the best way but this is the best option to avoid manual login to 3rd party to receive refresh token and to avoid multiple changes in integration. 

Can you use this to generate the refresh token?

 

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0791131

 

Description

Below is sample script Script to retrieve Access and Refresh tokens using GlideOAuthClient libraries

This could be run from background scripts for testing purpose

Instructions


Code to retrieve Access token and Refresh Token:

var oAuthClient = new sn_auth.GlideOAuthClient();
var requestor_context = 'test';
var requestor_id = 'abc@xyz.com';
var oauth_profile_id = '43d6bab3db849f009a6ff9b61d961957'; // profile ID [sys_id of  'OAuth Entity Profiles' (oauth_entity_profile) record in OAUTH registry  record]

var params = {grant_type:"password", username:'admin', password:'pwd', 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());


Code to retrieve a new Access Token using Refresh token

var oAuthClient = new sn_auth.GlideOAuthClient();
var requestor_context = 'test';
var requestor_id = 'abc@xyz.com';
var oauth_profile_id = '43d6bab3db849f009a6ff9b61d961957'; // profile ID [sys_id of  'OAuth Entity Profiles' (oauth_entity_profile) record in OAUTH registry  record]

var params = {grant_type:"refresh_token", refresh_token:"<value_of_refresh_token>", 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());

 

Making an outbound REST call with the retrieved token

// make the outbound REST call with the retrieved token

var r = new sn_ws.RESTMessageV2('empukemburu03_outbound', 'Default GET'); 
//setting oauth profile and oauth requester profile
r.setAuthenticationProfile('oauth2', oauth_profile_id); 
r.setRequestorProfile(requestor_context, requestor_id);
 
var response = r.execute(); 
var responseBody = response.getBody(); 
var httpStatus = response.getStatusCode(); 
 
gs.log(responseBody);

Hi Mike, 

I tried to use this and with grant type: password I just receiving access token without refresh token