How to get Auth Token using script when grant type is Authorization code
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2019 02:56 AM
Hi All,
I have set up a REST Message outbound using OAuth 2.0.
Accordingly i have set up OAuth Profile
I am able to get the token access token using 'Get OAuth Token' link in REST Outbound. I want to implement this in script. I have been trying the following code.
var tokenRequest = new sn_auth.GlideOAuthClientRequest();
tokenRequest.setGrantType("authorization_code");
var oAuthClient = new sn_auth.GlideOAuthClient();
var tokenResponse = oAuthClient.requestTokenByRequest("Google Entity profiles", 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());
However it is not working. I am getting the following response
failed to find the oauth entity.
*** Script: Error:failed to find the oauth entity.
*** Script: AccessToken:null
*** Script: AccessTokenExpiresIn:0
*** Script: RefreshToken:null
Please can someone suggest the correct syntax to get Authorization code for grant type as authorization code. I can only find the syntax for grant type password nut not for authorization code.
Note: Grant type is not password.
Help is much appreciated. Thanks!
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2019 03:09 AM
Can you call this and check what is the grant type you are getting
var tokenRequest = new sn_auth.GlideOAuthClientRequest();
gs.info("Grant type is "+tokenRequest.getGrantType());
Mark the comment as a correct answer and helpful if this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2019 03:36 AM
Hi Asif
I executed the line. O/P is
Grant Type:undefined
Where can i find the correct value for authorization code? and O/P also includes
Script: Error:failed to find the oauth entity
The entity profile do exist.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2019 03:40 AM
so thats the issue.
Generally grant type will be set in the oauth provider profile. If that is not, then you need to use setGrantType.
Can you check in your profile if its set or not.
if not, then call setGrantType and then again call call getGrantType and check if the value is set or not.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2019 03:51 AM