Setting outbound REST API with OAuth
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-17-2022 08:29 AM
Hi,
I am trying to integrate with third party with OAuth 2.0. We are using grant type as client credentials.
I need to pass bearer token every time in request header. I am trying with below script. Kindly help me on this.
I am getting access token as null.
(function executeRule(current, previous /*null when async*/) {
var oAuthClient = new sn_auth.GlideOAuthClient();
var params = {grant_type:"client_credentials", client_id:'xxxx', client_secret:'xxxx'};
var json = new global.JSON();
var text = json.encode(params);
var tokenResponse = oAuthClient.requestToken('xxx', text);
var token = tokenResponse.getToken();
gs.log("AccessToken:" + token.getAccessToken());
gs.log("AccessTokenExpiresIn:" + token.getExpiresIn());
gs.log(" RefreshToken:" + token.getRefreshToken());
var r = new sn_ws.RESTMessageV2('Oracle Service', 'post');
r.setRequestHeader("Content-Type","application/json");
r.setRequestHeader("Bearer Token"+accessToken);
r.setAuthentication("oauth2", "xxx default_profile");
r.setStringParameter("action","create");
r.setStringParameter("number",current.number);
r.setStringParameter("description",current.description);
r.setStringParameter("priority",current.priority);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
var jsonObject = JSON.parse(responseBody);
gs.log(httpStatus);
}
catch(ex) {
var message = ex.message;
gs.print("ex==:" + ex);
}
})(current, previous);
Thanks in advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-17-2022 02:12 PM
Hi, without details of your debugging or response info from the target application it is difficult to be sure but you appear to be submitting your token request without including full user authentication and while your target platform as it's not made clear, SNC and other API's I have worked with would require username and password as params, as well as client_id and client_secret, for token authentication.\
I recommend testing using an app like Postman as it will give you better visibility of target platform responses and then once you have your requirements correct and working, map these to your ServiceNow code.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-17-2022 10:38 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-19-2022 01:48 AM
When you use postman I assume that you pass in username and password as parameters, as well as client_id and client_secret? Yet (as I mentioned in above post) you do not appear to be passing these parameters in you posted code? What happens if you include username and password in your params object?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-17-2022 10:38 PM
Hi
Could you please help me on this.
Thanks.