How to deal with OAUTH Grant Type Authorization_code dynamically

harishdasari
Tera Guru

Hi,

we are doing the integration with an external application(Not ServiceNow) and it supports only OAuth 2.0 authentication.

And the Grant Type is "Authorization_Code".

we have configured the OAuth in ServiceNow --> Application Registry -->  Connect to a third party OAuth Provider

Currently what we are doing is using REST API GET operation and using the below format to retrieve the Authorization code.

https://XXXX/oauth/authorize?response_type=code&client_id=xyz123&redirect_uri=https//www.dev00000.service-now.com/oauth_redirect.do

It is showing the status as 200 successful, but it is not returning the Authorization code in response.

If I hit this link in the browser then it is returning the authorization code like below

https://dev0000.service-now.com/oauth_redirect.do?code=kHGRWTnBuV__sKZpD1UQAaR36-8gIyrMw

Now the issue is, Every time we need to use the new Authorization Code to retrieve the new access token from external application.

 

How can we handle this type of dynamic Auth code?

Any help will be appreciated.

Thank you.

2 REPLIES 2

Anil Lande
Kilo Patron

Hi,

I am having same issue for one of our integration, we want to generate refresh token and access token using script.

Can you please share if you have found any solution for this?

Thanks in advance!

Anil

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Hi, have you added an OAuth API Script to your application registry record?

https://docs.servicenow.com/bundle/paris-application-development/page/app-store/dev_portal/API_reference/OAuthUtil/concept/c_OAuthUtilScriptInclude.html

To do this you should make a copy of the OOB OAuthUtil record, not use the OOB record

/nav_to.do?uri=sys_script_include.do?sys_id=3e3a3a11c333210016194ffe5bba8f70

You can then update your version of the script to modify inbound\outbound token handling for your integration.
You can also do useful things like log the received\response payload;  to do this under line 24
var tokenResponse = (new global.JSON()).decode(accessTokenResponse.getBody());
//just add some logging IE.

gs.info('Token ' + tokenResponse.toString());