Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Unable to generate access token (using OAuth) from script.

Rohan Saxena2
Kilo Contributor

I am using the below code:

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

var oAuthClient = new sn_auth.GlideOAuthClient();
var tokenResponse = oAuthClient.requestTokenByRequest("<Oauth provider name>", 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());

And the output is : 

OAuthProblemException{error='Invalid request', description='null', uri='null', state='null', scope='null', redirectUri='null', responseStatus=0, parameters={}}
*** Script: Error:Invalid request
*** Script: AccessToken:null
*** Script: AccessTokenExpiresIn:0
*** Script: RefreshToken:null

I am able to generate the code/token from REST Message when I click "Get OAuth Token" UI action there but not from this script.

Also the code works while usinf SN to SN authorization, but I am trying with another 3rd party tool.

Does anyone has any ideas about what I might be missing, or a different approach for it.

 

Thanks

 

7 REPLIES 7

have you checked the below thread ? see if it helps you.  

 

 

https://community.servicenow.com/community?id=community_blog&sys_id=a1fce2a5dbd0dbc01dcaf3231f961939

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Rohan,

Have you given proper application registry name in the method requestTokenByRequest

Regards
Ankur

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

Yes it is correct else it throws an error stating "Couldn't find the OAuth Profile".