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-20-2020 11:30 PM
adding sample code. see if it helps you.
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());

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-20-2020 11:50 PM
Thanks, I tried this code, I don't really have the scope details so I had to leave it out.
Now it says
OAuthProblemException{error='invalid_grant', description='null', uri='null', state='null', scope='null', redirectUri='null', responseStatus=0, parameters={}}
*** Script: Error:invalid_grant
Evaluator: org.mozilla.javascript.EcmaError: Cannot convert null to an object. Caused by error in script at line 15 12: gs.info("Error:" + tokenResponse.getErrorMessage()); 13: 14: var token = tokenResponse.getToken(); ==> 15: gs.log("AccessToken:" + token.getAccessToken()); 16: gs.log("AccessTokenExpiresIn:" + token.getExpiresIn()); 17: gs.log(" RefreshToken:" + token.getRefreshToken());

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-20-2020 11:52 PM
if you are in scoped app, can you replace log to info

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-21-2020 05:39 AM
No I am not in a scoped application.
My question is, how did you get the authorization_code?
via browser I think, that's the way I can generate.
But can you achieve it without browser like through some web service call.
Because I can't ask an end user to pop a browser and fetch the code from there.
Thanks.