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

Harsh Vardhan
Giga Patron

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());

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());

if you are in scoped app, can you replace log to info 

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.