The CreatorCon Call for Content is officially open! Get started here.

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

Thanks! guys. I was able to figure it out finally.

I did generated a authorization code from api(created a new REST (GET)) and used the below code to get the token

var oAuthClient = new GlideOAuthClient();
var params = {
    'grant_type': "authorization_code",
    redirect_uri: 'https://houzecheck.service-now.com/oauth_redirect.do',
    'scope': 'https://www.googleapis.com/auth/calendar'
'code': authorizaton_code;
};
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());

 

This returned the token.

Hi Rizwan,

Can you explain about the parameters you sent in the parms?

grant_type -> authorization_code

what about redirect_uri and scope? what does it mean?

Can you share screenshot when you run the script in background?

It gave me an error when I tried similar script at my side

Regards

Ankur

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

Hi Ankur

 

redirect_uri is system generated uri (You can create your own too ex: UI page). This serves as a page where the response will be sent back. In this case authorization code will be sent back after appending to the url.

ex: https://houzecheck.service-now.com/oauth_redirect.do?code=4/tQGV4IydZS1JPu8nbHW4u3w9PlA3klDUqSpQWSq4...

Scope - The resource you are trying to access. In my case it was Event. I wanted to generate an event in the Google Calender API.

Grant type - I wanted the token. So in order get the token i used the grant type as authorization code. Google API demands authorization code in order return a token. Hence i set grant type as authorization code and within code param passed on the authorization code value.

When i executed the above code with the params. I got the token.

Then invoked the REST, (POST)Create event method and passed this token in Header as Authorization.

Scope can be ignored. However Redirect Uri is required.

Hi Rizwan,

Thanks for the information. When I tried the above script it gave me an error "

Error:invalid_request, Missing parameters: access_token

"

So wanted to check whether you were able to receive the token or not.

Or may be the code will work only for your instance since that might be the configuration. 

Regards

Ankur

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

Hi Ankur,

Apologies, i ma a bit late. Was busy with the further development.

Here is the code

and O/P

 

var oAuthClient = new GlideOAuthClient();
var params = {
'grant_type': "authorization_code",
'redirect_uri': 'https://houzecheck.service-now.com/oauth_redirect.do',
//'scope': 'https://www.googleapis.com/auth/calendar',
'code': '4/uAH55MWFbiHaBFUoo5pLNh5PSu5RK1kB-Ma3zLc0PBves1n5YW6iWtFYrsqhZbcGNsymal8Ltn603q3Yc-Y-H0A'
};
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());

O/P

*** Script: Error:null
*** Script: AccessToken:ya29.ImO0Bwh7uCWs0Q8q1slp1hYAhPM3adNrPu49P_SJwDQ1ik9ismMcy86A5foOf8WF_y5V4zGOhrYIgtJTps2c8ag4iSQTIEvdq59k1EL5DFYV8xmlbnk6Ly7hNahBPwM7Ql1scVw
*** Script: AccessTokenExpiresIn:3590
*** Script:  RefreshToken:null

 

This is working in my instance because i have setup the Application Registry accordingly.