Connecting to Incoming Scripted API with Java code

samiroy
Kilo Expert
Hi SN Community,
 
Need a bit of technical help . We are trying to write a sample Java code to make an inbound scripted api call and are getting stuck. 
 
we used documentation here 
 
We set up a scripted API and tested it using SN Rest API explorer
 
image.png

 
We know the rest api call works because we get the response : 
 
image.png
 
So, using the documentation / the rest api  code sample provided, we wrote our java code. 
 

  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();
	    }
		
	}

}



















  
 
However we are getting the error 
 
 
image.png
 
Appreciate your help,
 
 


 
Best Regards,
 
Sami Roy
 
 
 
1 ACCEPTED SOLUTION

samiroy
Kilo Expert

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

View solution in original post

8 REPLIES 8

ARG645
Tera Guru

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

samiroy
Kilo Expert

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

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. 

Hi, samiroy,
 
can u pls provide the piece of code for the POST API in the above pattern,
 
for accessing scripted rest api
 
 
thanks in advance