- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
â07-08-2020 09:59 PM
Experts,
I am working on an integration between servicenow and another case management application. I am setting up OAuth integration. I am successful in setting up with Grant type as JWT Bearer token. Now I need to do it with Grant type as password.
I did the initial OAuth Configurations in servicenow and is successful in setting up the REST message which successfully gives me Access Code.
I am having trouble in Automating this via script.
var payload = {
"grant_type": gs.getProperty('x_1234_sf.sdc_oauth_grant_type').toString(),
"username": gs.getProperty('x_1234_sf.sdc_oauth_user').toString(),
"password": gs.getProperty('x_1234_sf.sdc_oauth_user_pwd').toString(),
"client_id": gs.getProperty('x_1234_sf.sdc_client_id').toString(),
"client_secret": gs.getProperty('x_1234_sf.sdc_client_secret').toString()
};
Need to pass this payload into the rest message
try {
var r = new sn_ws.RESTMessageV2();
r.setHttpMethod("POST");
r.setEndpoint(gs.getProperty('x_1234_sf.sdc_oauth_end_point'));
r.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
r.setRequestBody(< how do I formulate the request body ?? >);
this.response = r.executeAsync();
this.response.waitForResponse(5);
this.httpResponseStatus = this.response.getStatusCode();
this.responseBody = this.response.getBody();
var obj = JSON.parse(this.responseBody);
this.result = {
"resp_code": this.httpResponseStatus,
"a_token": obj['access_token'],
"type_token": obj['token_type']
};
Please provide some guidance.
thx
ram.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
â07-09-2020 01:38 PM
Thank you for the reply Ankur.
Figured out the fix for this issue.
What I was doing wrong is formulating the request Body while calling the OAuth API end point.
For Resource credentials grant type, the request body should be like as in the below script.
I do not have to use any of the GlideOAuth related methods.
var req = new sn_ws.RESTMessageV2();
req.setEndPoint( < OAUTh end point URL >);
req.setHttpMethod('POST');
req.setRequestHeader('content-type','application/x-www-form-urlencoded')
req.setRequestBody(grant_type=" + grant_type + "&username=" + username + "&password="+encodeURI(pwd)+ "&client_id="+client_id+"+ "&client_secret="+client_secret));
var response = req.ExecuteAsync();
response.WaitForResponse(10);
var responseBody = response.getBody();
var obj = JSON.parse(responseBody);
gs.info('Token : "+obj.access_token);
With the above script, I am able to get the access code without converting the biz rule to Async & display the info message to the end user.
thx
ram.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
â07-09-2020 01:37 AM
Hi,
I could see you are using the system property in scope 1234
from background script are you running this from global scope or your custom scope
I think it is not allowing the RestMessage call from UI action. It is telling to use async business rule
Can you inform what that UI action is doing? Can you move the code to async BR based on some condition
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
â07-09-2020 12:43 AM
From UI action of an incident , am inserting the record into staging table . A business rule on the staging table ( After Insert ), makes the first call to get access token . Once I get the access token, I make a REST call (executeAsync) to third party end point.
And Application log errors as below,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
â07-09-2020 01:06 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
â07-09-2020 02:08 AM
If I conver the biz rule to an Async , it all works.
If I convert this into Async business rule, i wouldnt be able to show the end user the message with the Case ID generated on the target case system.
Any way to do this ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
â07-09-2020 02:20 AM
Hi Ram,
that's correct. one workaround for this would be below
1) I assume you must be storing the Target Case system in some field
2) next time when user opens the record/form you can show the message
Another approach
1) Put the code to trigger REST Message in script action
2) create event in event table and trigger event from BR; once event is processed it would make the API Call
3) in after insert/update BR you can trigger the event and show the message as well; it won't contain the case number but a generic message you can give that
"Case creation has been initiated. Please visit this record in some time to check the case number"
Script of BR:
gs.addInfoMessage('Your Message Here');
gs.eventQueue('event_name', current, '','');
Regards
Ankur
Ankur
⨠Certified Technical Architect || ⨠9x ServiceNow MVP || ⨠ServiceNow Community Leader