How to get Auth Token using script when grant type is Authorization code

Rizwan Shaikh
Mega Guru

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.

find_real_file.png

Note: Grant type is not password.

Help is much appreciated. Thanks!

32 REPLIES 32

asifnoor
Kilo Patron

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.

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.

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.

Grant type is already set.

 

Somehow the script is not able to find the profile itself.

That's the reason for grant type is undefined.