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 05:07 AM
As of now i am executing the script in background script. SO it is global scope.
The link i already have. It has the example for grant type password. I am trying for Authorization code.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2019 05:18 AM
Hi Rizwan,
Can you update code as below and try once
give the name of the application registry in this method as first parameter and not the entity profile name
var tokenResponse = oAuthClient.requestTokenByRequest("", tokenRequest);
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2019 05:44 AM
I tried the following code after replacing the entity profile with Application Registry
var tokenRequest = new sn_auth.GlideOAuthClientRequest();
tokenRequest.setGrantType("authorization_code");
tokenRequest.setScope('https://www.googleapis.com/auth/calendar');
var oAuthClient = new sn_auth.GlideOAuthClient();
var tokenResponse = oAuthClient.requestTokenByRequest("Google", tokenRequest);
gs.info("Error:" + tokenResponse.getErrorMessage());
var token = tokenResponse.getToken();
gs.info("Grant Type:" + token.getGrantType());
gs.info("AccessToken:" + token.getAccessToken());
gs.info("AccessTokenExpiresIn:" + token.getExpiresIn());
gs.info("RefreshToken:" + token.getRefreshToken());
Looks like the script was able to find the Registry. But i am getting the following error which is missing parameters.
OAuthProblemException{error='invalid_request', description='Missing required parameter: code', uri='null', state='null', scope='null', redirectUri='null', responseStatus=0, parameters={}}
*** Script: Error:invalid_request, Missing required parameter: code
*** Script: Grant Type:undefined
*** Script: AccessToken:null
*** Script: AccessTokenExpiresIn:0
*** Script: RefreshToken:null
Ideally all the parameters should be considered from the registry record. shouldn't they?
Also, I was checking the methods list for GlideOAuthClientRequest to set the missing params. Not all the methods are available for ex: set uri, set redirect Uri etc..
I am sure there must be a away but no idea yet.
Please, could you suggest something.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2019 05:50 AM
Hi Rizwan,
Can you please check below links:
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2019 07:00 AM
Hi Ankur,
Nope this doesnt help. The Link which you mentioned has the same url for grant type password one. Nothing for authorization code. I even tried setting the parameters as below.
var oAuthClient = new GlideOAuthClient();
var params = {
'grant_type': "authorization_code",
'redirectUri': 'https://houzecheck.service-now.com/oauth_redirect.do',
'scope': 'https://www.googleapis.com/auth/calendar',
};
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());
Following is the error
OAuthProblemException{error='invalid_request', description='Missing required parameter: code', uri='null', state='null', scope='null', redirectUri='null', responseStatus=0, parameters={}}
*** Script: Error:invalid_request, Missing required parameter: code
It seems this process needs authorization code to get the token.
My Case is to get the authorization code.
Get the Token using the authorization code.
Make the REST call using the Token.