How to get Auth Token using script when grant type is Authorization code

Rizwan Shaikh
Mega Guru

Hi All,

I have set up a REST Message outbound using OAuth 2.0. 

Accordingly i have set up OAuth Profile

I am able to get the token access token using 'Get OAuth Token' link in REST Outbound. I want to implement this in script. I have been trying the following code. 

var tokenRequest = new sn_auth.GlideOAuthClientRequest();
tokenRequest.setGrantType("authorization_code");

var oAuthClient = new sn_auth.GlideOAuthClient();
var tokenResponse = oAuthClient.requestTokenByRequest("Google Entity profiles", tokenRequest);
gs.info("Error:" + tokenResponse.getErrorMessage());

var token = tokenResponse.getToken(); 
gs.info("AccessToken:" + token.getAccessToken());
gs.info("AccessTokenExpiresIn:" + token.getExpiresIn());
gs.info("RefreshToken:" + token.getRefreshToken());

However it is not working. I am getting the following response

failed to find the oauth entity.
*** Script: Error:failed to find the oauth entity.
*** Script: AccessToken:null
*** Script: AccessTokenExpiresIn:0
*** Script: RefreshToken:null

Please can someone suggest the correct syntax to get Authorization code for grant type as authorization code. I can only find the syntax for grant type password nut not for authorization code.

find_real_file.png

Note: Grant type is not password.

Help is much appreciated. Thanks!

32 REPLIES 32

As of now i am executing the script in background script. SO it is global scope.

 

The link i already have. It has the example for grant type password. I am trying for Authorization code.

Hi Rizwan,

Can you update code as below and try once

give the name of the application registry in this method as first parameter and not the entity profile name

var tokenResponse = oAuthClient.requestTokenByRequest("", tokenRequest);

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

 

I tried the following code after replacing the entity profile with Application Registry

var tokenRequest = new sn_auth.GlideOAuthClientRequest();
tokenRequest.setGrantType("authorization_code");
tokenRequest.setScope('https://www.googleapis.com/auth/calendar');


var oAuthClient = new sn_auth.GlideOAuthClient();
var tokenResponse = oAuthClient.requestTokenByRequest("Google", tokenRequest);

gs.info("Error:" + tokenResponse.getErrorMessage());

var token = tokenResponse.getToken(); 
gs.info("Grant Type:" + token.getGrantType());
gs.info("AccessToken:" + token.getAccessToken());
gs.info("AccessTokenExpiresIn:" + token.getExpiresIn());
gs.info("RefreshToken:" + token.getRefreshToken());

 

Looks like the script was able to find the Registry. But i am getting the following error which is missing parameters.

OAuthProblemException{error='invalid_request', description='Missing required parameter: code', uri='null', state='null', scope='null', redirectUri='null', responseStatus=0, parameters={}}
*** Script: Error:invalid_request, Missing required parameter: code
*** Script: Grant Type:undefined
*** Script: AccessToken:null
*** Script: AccessTokenExpiresIn:0
*** Script: RefreshToken:null

Ideally all the parameters should be considered from the registry record. shouldn't they?

 

Also, I was checking the methods list for GlideOAuthClientRequest to set the missing params. Not all the methods are available for ex: set uri, set redirect Uri etc..

 

I am sure there must be a away but no idea yet.

 

Please, could you suggest something.

Hi Rizwan,

Can you please check below links:

https://community.servicenow.com/community?id=community_question&sys_id=4ca5cbeddbd8dbc01dcaf3231f96...

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,

 

Nope this doesnt help. The Link which you mentioned has the same url for grant type password one. Nothing for authorization code. I even tried setting the parameters as below.

var oAuthClient = new GlideOAuthClient();
var params = {
    'grant_type': "authorization_code",
    'redirectUri': 'https://houzecheck.service-now.com/oauth_redirect.do',
    'scope': 'https://www.googleapis.com/auth/calendar',
};
var json = new JSON();
var text = json.encode(params);
var tokenResponse = oAuthClient.requestToken('Google', text);
gs.info("Error:" + tokenResponse.getErrorMessage());

var token = tokenResponse.getToken();
gs.log("AccessToken:" + token.getAccessToken());
gs.log("AccessTokenExpiresIn:" + token.getExpiresIn());
gs.log(" RefreshToken:" + token.getRefreshToken());

Following is the error

OAuthProblemException{error='invalid_request', description='Missing required parameter: code', uri='null', state='null', scope='null', redirectUri='null', responseStatus=0, parameters={}}
*** Script: Error:invalid_request, Missing required parameter: code

It seems this process needs authorization code to get the token.

My Case is to get the authorization code.

Get the Token using the authorization code.

Make the REST call using the Token.