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 09:13 AM
Thanks! guys. I was able to figure it out finally.
I did generated a authorization code from api(created a new REST (GET)) and used the below code to get the token
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());
This returned the token.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2019 09:22 AM
Hi Rizwan,
Can you explain about the parameters you sent in the parms?
grant_type -> authorization_code
what about redirect_uri and scope? what does it mean?
Can you share screenshot when you run the script in background?
It gave me an error when I tried similar script at my side
Regards
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 09:41 AM
Hi Ankur
redirect_uri is system generated uri (You can create your own too ex: UI page). This serves as a page where the response will be sent back. In this case authorization code will be sent back after appending to the url.
Scope - The resource you are trying to access. In my case it was Event. I wanted to generate an event in the Google Calender API.
Grant type - I wanted the token. So in order get the token i used the grant type as authorization code. Google API demands authorization code in order return a token. Hence i set grant type as authorization code and within code param passed on the authorization code value.
When i executed the above code with the params. I got the token.
Then invoked the REST, (POST)Create event method and passed this token in Header as Authorization.
Scope can be ignored. However Redirect Uri is required.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2019 09:31 PM
Hi Rizwan,
Thanks for the information. When I tried the above script it gave me an error "
Error:invalid_request, Missing parameters: access_token
"
So wanted to check whether you were able to receive the token or not.
Or may be the code will work only for your instance since that might be the configuration.
Regards
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
12-09-2019 08:23 AM
Hi Ankur,
Apologies, i ma a bit late. Was busy with the further development.
Here is the code
and O/P
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': '4/uAH55MWFbiHaBFUoo5pLNh5PSu5RK1kB-Ma3zLc0PBves1n5YW6iWtFYrsqhZbcGNsymal8Ltn603q3Yc-Y-H0A'
};
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());
O/P
*** Script: Error:null
*** Script: AccessToken:ya29.ImO0Bwh7uCWs0Q8q1slp1hYAhPM3adNrPu49P_SJwDQ1ik9ismMcy86A5foOf8WF_y5V4zGOhrYIgtJTps2c8ag4iSQTIEvdq59k1EL5DFYV8xmlbnk6Ly7hNahBPwM7Ql1scVw
*** Script: AccessTokenExpiresIn:3590
*** Script: RefreshToken:null
This is working in my instance because i have setup the Application Registry accordingly.