How to to test Auth grant type using the sn_auth - oAuth API for outbound rest message.


Description

Description


This article explains the steps to test Auth grant type using the sn_auth - oAuth API for outbound rest message.

Procedure


1.Run this script in the scripts background.

 

/**** 
This scripts is useful to test client credential via glide scripting. 
Prerequisites:Configure OAuth entity with type OAuth provider."Azure AD" is the OAuth Provider. 

curl -d "grant_type=authorization_code&code=uDFbKLcxsgdwPL4afMqAZIWPUNQAIHFxEtMw0U7rPBCAGDTK9_3vBUHIRCrdKOdivvHFQrr42bzJ743ufNpPQw&client_id=e7670e22fdeb1300091ee995affe2247&client_secret=K2::;]A|~3&redirect_uri=https%3A%2F%2Fempimranitsm.service-now.com%2Foauth_redirect.do" https://empimranitom.service-now.com/oauth_token.do 

***/ 

AuthFlow();
function AuthFlow(){ 

var tokenRequest = new sn_auth.GlideOAuthClientRequest(); 
tokenRequest.setGrantType("authorization_code"); 
tokenRequest.setParameter("grant_type","authorization_code"); 
tokenRequest.setParameter("code","uDFbKLcxsgdwPL4afMqAZIWPUNQAIHFxEtMw0U7rPBCAGDTK9_3vBUHIRCrdKOdivvHFQrr42bzJ743ufNpPQw"); 
tokenRequest.setParameter("client_id","e7670e22fdeb1300091ee995affe2247"); 
tokenRequest.setParameter("client_secret","K2::;]A|~3"); 
tokenRequest.setParameter("redirect_uri","https://empimranitsm.service-now.com/oauth_redirect.do"); 




var oAuthClient = new sn_auth.GlideOAuthClient(); 

//Retrieves the token for the client and the request set into a GlideOAuthClientResponse object. 
var tokenResponse = oAuthClient.requestTokenByRequest("Google Contacts API Integration", tokenRequest); 

//Prints GlideOAuthClientResponse object. 
gs.info("Prints GlideOAuthClientResponse object members"); 
gs.info("Error:" + tokenResponse.getErrorMessage()); 
gs.info("Token Response Body:" + tokenResponse.getBody()); 
gs.info("Token Response Content Type:" + tokenResponse.getContentType()); 
gs.info("Token Response Code:" + tokenResponse.getResponseCode()); 
gs.info("Token Response Parameters:" + tokenResponse.getResponseParameters()); 

var paramMap = tokenResponse.getResponseParameters() 

gs.info("Iterating Token Response Parameters Starts"); 
for (param in paramMap){ 

gs.info("Key:"+param+" "+"value:"+paramMap[param].toString()); 
//paramMap.put(param, tokenResponse2[param].toString()); 

gs.info("Iterating Token Response Parameters Ends"); 

//This Returns GlideOAuthToken object 
var token = tokenResponse.getToken(); 
dumpToken(token); 



function dumpToken(token) { 
if(token) { 
gs.info("Prints Glide oAuthToken Object Members"); 
gs.info("AccessToken:" + token.getAccessToken()); 
gs.info("AccessTokenExpiresIn:" + token.getExpiresIn()); 
gs.info("Access Token SysID:" + token.getAccessTokenSysID()); 
gs.info("Access Token Scope:" + token.getScope()); 


}

Applicable Versions


This script was tested in the Kingston release.It should work in any release which supports Auth grant type.

Additional Information


1)Doc site

https://docs.servicenow.com/search?q=OAuth+2.0

 
 


Description


This article explains the steps to test Auth grant type using the sn_auth - oAuth API for outbound rest message.

Procedure


1.Run this script in the scripts background.

 

/**** 
This scripts is useful to test client credential via glide scripting. 
Prerequisites:Configure OAuth entity with type OAuth provider."Azure AD" is the OAuth Provider. 

curl -d "grant_type=authorization_code&code=uDFbKLcxsgdwPL4afMqAZIWPUNQAIHFxEtMw0U7rPBCAGDTK9_3vBUHIRCrdKOdivvHFQrr42bzJ743ufNpPQw&client_id=e7670e22fdeb1300091ee995affe2247&client_secret=K2::;]A|~3&redirect_uri=https%3A%2F%2Fempimranitsm.service-now.com%2Foauth_redirect.do" https://<instancename>.service-now.com/oauth_token.do 

***/ 

AuthFlow();
function AuthFlow(){ 

var tokenRequest = new sn_auth.GlideOAuthClientRequest(); 
tokenRequest.setGrantType("authorization_code"); 
tokenRequest.setParameter("grant_type","authorization_code"); 
tokenRequest.setParameter("code","uDFbKLcxsgdwPL4afMqAZIWPUNQAIHFxEtMw0U7rPBCAGDTK9_3vBUHIRCrdKOdivvHFQrr42bzJ743ufNpPQw"); 
tokenRequest.setParameter("client_id","e7670e22fdeb1300091ee995affe2247"); 
tokenRequest.setParameter("client_secret","K2::;]A|~3"); 
tokenRequest.setParameter("redirect_uri","https://<instancename>.service-now.com/oauth_redirect.do"); 




var oAuthClient = new sn_auth.GlideOAuthClient(); 

//Retrieves the token for the client and the request set into a GlideOAuthClientResponse object. 
var tokenResponse = oAuthClient.requestTokenByRequest("Google Contacts API Integration", tokenRequest); 

//Prints GlideOAuthClientResponse object. 
gs.info("Prints GlideOAuthClientResponse object members"); 
gs.info("Error:" + tokenResponse.getErrorMessage()); 
gs.info("Token Response Body:" + tokenResponse.getBody()); 
gs.info("Token Response Content Type:" + tokenResponse.getContentType()); 
gs.info("Token Response Code:" + tokenResponse.getResponseCode()); 
gs.info("Token Response Parameters:" + tokenResponse.getResponseParameters()); 

var paramMap = tokenResponse.getResponseParameters() 

gs.info("Iterating Token Response Parameters Starts"); 
for (param in paramMap){ 

gs.info("Key:"+param+" "+"value:"+paramMap[param].toString()); 
//paramMap.put(param, tokenResponse2[param].toString()); 

gs.info("Iterating Token Response Parameters Ends"); 

//This Returns GlideOAuthToken object 
var token = tokenResponse.getToken(); 
dumpToken(token); 



function dumpToken(token) { 
if(token) { 
gs.info("Prints Glide oAuthToken Object Members"); 
gs.info("AccessToken:" + token.getAccessToken()); 
gs.info("AccessTokenExpiresIn:" + token.getExpiresIn()); 
gs.info("Access Token SysID:" + token.getAccessTokenSysID()); 
gs.info("Access Token Scope:" + token.getScope()); 


}

Applicable Versions


This script was tested in the New York release.It should work in any release which supports Auth grant type.

Additional Information


1)Doc site

https://docs.servicenow.com/search?q=OAuth+2.0