OAuth 2.0 - Unable to generate Refresh Token - Cannot convert null to an object

Sobha1
Tera Contributor

Hi All,

Using the below script, I am trying to generate the OAuth Refresh Token, I am getting the error - Cannot convert null to an object.

I verified the below UN and Pwd using Postman, everything is fine. In postman app, I could generate the refresh token and access token without any issues. 

var oauthC = new GlideOAuthClient();
var param = {grant_type:"password",username:"OAuth",password:"Welcome@123"};
var jsonP = new global.JSON();
var tResponse = oauthC.requestToken('DemoOauth',jsonP.encode(param));
var Token = tResponse.getToken().getRefreshToken();
gs.print("Refresh Token -"+Token );

Any inputs are highly appreciated. 

2 REPLIES 2

Sagar Pagar
Tera Patron

Hi @Sobha1,

 

Try this updated scripts.

var oauthC = new sn_auth.GlideOAuthClient();
var param = {grant_type:"password",username:"OAuth",password:"Welcome@123"};
gs.info("param- " + param);

var jsonP = new global.JSON();
var jsonParam = jsonP.encode(param);
gs.info("jsonParam- " + jsonParam);

var tResponse = oauthC.requestToken('DemoOauth', jsonParam);
gs.info("tResponse- " + tResponse);

// var Token = tResponse.getToken();
// gs.info("Refresh Token- " + Token);

var Token = tResponse.getToken().getRefreshToken();
gs.info("Refresh Token- " + Token);

 


If my response helps to solve your issue. Kindly mark it as helpful & correct. It will be helpful for future readers! 👍🏻
Thanks,
Sagar Pagar

The world works with ServiceNow

Hi Sagar,

Thanks for your prompt response. Below is the output screenshot for the above script from background script

Sobha1_1-1692508949432.png