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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-20-2020 11:23 PM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-21-2020 05:50 AM
have you checked the below thread ? see if it helps you.
https://community.servicenow.com/community?id=community_blog&sys_id=a1fce2a5dbd0dbc01dcaf3231f961939
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-20-2020 11:32 PM
Hi Rohan,
Have you given proper application registry name in the method requestTokenByRequest
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-20-2020 11:48 PM
Yes it is correct else it throws an error stating "Couldn't find the OAuth Profile".