- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-08-2018 05:03 PM
import java.util.* ;
import java.sql.Date ;
import java.io.IOException ;
import org.apache.http.HttpException ;
import org.apache.http.HttpHost ;
import org.apache.http.auth.AuthScope ;
import org.apache.http.auth.UsernamePasswordCredentials ;
import org.apache.http.client.CredentialsProvider ;
import org.apache.http.client.methods.CloseableHttpResponse ;
import org.apache.http.client.methods.HttpGet ;
import org.apache.http.impl.client.BasicCredentialsProvider ;
import org.apache.http.impl.client.CloseableHttpClient ;
import org.apache.http.impl.client.HttpClients ;
import org.apache.http.util.EntityUtils ;
import org.apache.http.*;
import org.apache.*;
public class TestingSN {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("hello world");
String instance = "ven02444.service-now.com";
String snuser = “validIserid";
String snpwd = “validPwd”;
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(new HttpHost(instance)),
new UsernamePasswordCredentials(snuser, snpwd));
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.build();
try {
HttpGet httpget = new HttpGet("https://ven02444.service-now.com/api/x_tracl_tracecloud/getrequirement?projectPrefix=TC&reqFullTag=BR-923");
httpget.setHeader("Accept", "application/json");
httpget.setHeader("Content-Type", "application/json");
System.out.println("Executing request " + httpget.getRequestLine());
CloseableHttpResponse response2 = httpclient.execute(httpget);
try {
System.out.println("srt output is " + response2.toString());
} finally {
response2.close();
}
} catch(Exception e){
e.printStackTrace();
} finally {
//httpclient.close();
}
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-09-2018 09:47 PM
Hi All,
Finally got the code working. Huge Huge thanks to Jaya Bharati Tallada @ Service Now (part of the certification team). She is totally amazing.
If you are stuck in a similar situation, here is what you should do :
1. Remove all clutter from your script include. Keep it very simple, and make sure it's not doing any thing. It should just print a message acknowledging receipt. If you have to use tables, make sure your user (the one calling the api) belongs to a role that has read acls on this table.
2. Make sure you give Execute ACL on this scripted api to a role , the calling user is a member of.
Use the sample java code below :
package batchJobs.Temp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import com.oreilly.servlet.Base64Encoder;
public class testSNAPI {
// API Called: https://<instance>.service-now.com/api/now/table/incident?sysparm_limit=1
public static void main(String[] args) {
try {
URL url = new URL(
"https://ven022233334.service-now.com/api/x_tracl_tracecloud/getrequirement?projectPrefix=TC&reqFullTag=BR-923");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Content-Type", "application/json");
//String encoding = Base64Encoder.encode ("abel.tuter:abel.tuter");
String userCredentials = "snUsaerId:snPassword";
String basicAuth = "Basic " + new String(Base64Encoder.encode(userCredentials.getBytes()));
conn.setRequestProperty("Authorization",
basicAuth);
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Good Luck
Sami
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-08-2018 10:17 PM
The user account used in the request to authenticate yourself to Servicenow should have read access to the table/records that you are trying to fetch from the API call.
Does your user account have enough permissions to read the target tables and its data ? If not, please configure the ACL and roles for your user account to fetch information from the target tables
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-09-2018 09:47 PM
Hi All,
Finally got the code working. Huge Huge thanks to Jaya Bharati Tallada @ Service Now (part of the certification team). She is totally amazing.
If you are stuck in a similar situation, here is what you should do :
1. Remove all clutter from your script include. Keep it very simple, and make sure it's not doing any thing. It should just print a message acknowledging receipt. If you have to use tables, make sure your user (the one calling the api) belongs to a role that has read acls on this table.
2. Make sure you give Execute ACL on this scripted api to a role , the calling user is a member of.
Use the sample java code below :
package batchJobs.Temp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import com.oreilly.servlet.Base64Encoder;
public class testSNAPI {
// API Called: https://<instance>.service-now.com/api/now/table/incident?sysparm_limit=1
public static void main(String[] args) {
try {
URL url = new URL(
"https://ven022233334.service-now.com/api/x_tracl_tracecloud/getrequirement?projectPrefix=TC&reqFullTag=BR-923");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Content-Type", "application/json");
//String encoding = Base64Encoder.encode ("abel.tuter:abel.tuter");
String userCredentials = "snUsaerId:snPassword";
String basicAuth = "Basic " + new String(Base64Encoder.encode(userCredentials.getBytes()));
conn.setRequestProperty("Authorization",
basicAuth);
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Good Luck
Sami
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-09-2018 11:30 PM
Makes sense. I knew about the read access to the target tables, but Execute ACL on thie scripted api to a role is something new.
Thank you for sharing.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2021 05:37 AM