Setup OAuth2 authentication for RESTMessageV2 integrations
1. Configure OAuth provider on instance 1 (OAuth Application Registry -> Create an OAuth API endpoint for external clients)
- Create unique provider profile name.
- We need to generate client ID along with Client Secret. Both can be generated by system normally.
- Token lifespan are optional, generated by default system policy.
2. Configure OAuth consumer on instance 2 (OAuth Application Registry -> Connect to a third party OAuth Provider)
- Create unique consumer profile name. (very important, script will need pass in this consumer profile name as parameter)
- Client ID and Client secret are the values were generated from step 1.
- Grant type. Value can be either "password" or "refresh_token". Suggest to use password since you won't have refresh_token info initially. This refresh_token only will be generated during first time when access token is generated.
- Token URL will be provided by OAuth provider. In this example, it would be the https://oauth_provider_instance1.service-now.com/oauth_token.do
var oAuthClient = new sn_auth.GlideOAuthClient();
var params = {grant_type:"password", username:'user_id from provider that will grant OAuth access', password:'user_pwd from provider that will grant OAuth access'};
var json = new global.JSON();
var text = json.encode(params);
var tokenResponse = oAuthClient.requestToken('unique consumer profile name from step 2.1', text);
var token = tokenResponse.getToken();
gs.log("AccessToken:" + token.getAccessToken());
gs.log("AccessTokenExpiresIn:" + token.getExpiresIn());
gs.log(" RefreshToken:" + token.getRefreshToken());
//You should be getting proper Access Token long with Refresh Token info. This token will be used in future web service request.
4. Setup proper outbound message on consumer instance 2 to the endpoint on provider instance 1.
- In this REST example, choose OAuth 2.0 as authentication type.
- You may use UI action "Get OAuth Token" to test you are able to get token info successfully.
5. Test outbound REST message along with token generation script to Web Service provider/OAuth provider instance 1 (from OAuth consumer instance 2).
var r = new sn_ws.RESTMessageV2('P2 Incidents', 'get');
r.setStringParameter('priority', '2');
r.setStringParameter('active', 'true');
r.setStringParameter('sysparm_fields', 'number,state,priority');
//override authentication profile
//authentication type ='basic'/ 'oauth2'
//This line below is optional if you have configured OAuth as authentication type in your outbound REST
r.setAuthentication('oauth2', 'OAuth_Client1');
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.log(responseBody);
6. Special Case 1: In Fuji or earlier version, user doesn't have same menu as my Geneva screenshot
In this case, you can use REST OAuth API to call in to get issue token embedded, see my highlights.
var r = new sn_ws.RESTMessageV2('P2 Incidents', 'get');
r.setStringParameter('priority', '2');
r.setStringParameter('active', 'true');
r.setStringParameter('sysparm_fields', 'number,state,priority');
/*override authentication profile
authentication type ='basic'/ 'oauth2'
line below is optional if you have configured OAuth as authentication type in your outbound REST*/
r.setAuthentication('oauth2', 'OAuth_Client1');
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.log(responseBody);
7. Special Case 2: OAuth grant type is not 'password' or 'refresh_token'
ServiceNow only support 2 mentioned above grant type at the moment this document is created. In this case, you will have to work with OAuth provider to figure out what grant type can be used per provider, and then setup a separate WS call to request for token issuance. And then you can embed token string in your subsequent WS call's header till this token expires.
//Calling REST to get oauth token and type issued.
function requestToken() {
var r = new sn_ws.RESTMessageV2('Oauth Token Request', 'post');
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
var authString;
if (httpStatus == 200 && responseBody != " ") {
var obj = new JSON.parse(responseBody);
var token = obj.access_token;
var token_type = obj.token_type;
authString = token_type + " " + token;
} else {
gs.print("Cannot acquire token");
}
gs.print("Token Type + Token:\n" + authString);
return authString;
}
//Use tokenString to setup header
var tokenString = requestToken();
var r = new sn_ws.RESTMessageV2('P2 Incidents', 'get');
r.setStringParameter('priority', '2');
r.setStringParameter('active', 'true');
r.setStringParameter('sysparm_fields', 'number,state,priority');
r.setRequestHeader('Authorization', tokenString);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.log(responseBody);
Comments
Post a Comment