Access ServiceNow instance through a Java Program

vivektietkumar
Giga Contributor

I am trying to access the instance through a Java program and getting the 401 error, which means unauthorized access, but when I use the same credentials to access the instance, they work just fine. I double checked there are no ACLs restricting the access also. Any help is appreciated.

URL I am trying to access is - https://dev10672.service-now.com/api/now/table/x_53167_cloud_prov_cloud_request?sysparm_fields=techn...

Java Code I am using is

private String getServiceNowResponse() {

String output = null;

try {

                Client restclient = Client.create();

              String name = "";

              String password = "";

              String authstring = name + ":" + password;

              String URL = "https://dev10672.service-now.com/api/now/table/x_53167_cloud_prov_cloud_request?sysparm_fields=techn...";

           

           

                  String authStringEnc = new BASE64Encoder().encode(authstring.getBytes());

           

                  WebResource webResource = restclient.resource(URL);

                  ClientResponse response = webResource.accept("application/json")

                                    .header("Authorization","Basic" + authStringEnc ).get(ClientResponse.class);

        if (response.getStatus() != 200) {

              throw new RuntimeException("Failed : HTTP error code : "

        + response.getStatus());

}

output = response.getEntity(String.class);

Screen Print of ACLs - there is no Role specified in the Requires Role tab. Please see attachment.

Capture.JPG

The option of "Allow access to this table via web services" is also ticked.

Capture2.JPG

I gave the user and the table rest service role also, but that doesn't help also.

5 REPLIES 5

Domenic Horner
Tera Expert

Hey there,



Looking over your code, it looks as if you're using the Java `jersey-client` library?


I cant find any direct documentation about how HTTP Basic Auth works within this library, outside of this section of their docs.


(If this isn't the client you're using, please let me know)



I also found this method of adding a username/password to your Jersey request:


Client c = Client.create();
c.addFilter(new HTTPBasicAuthFilter(user, password));


Have you tried a simple request wit the inbuilt HTTPClient library?



I would like to narrow down the cause of the 401, which could be because the username and password pair is not being sent correctly.



Since you are in a scoped application, please also make sure this user has the correct permissions, also test this integration with the admin user on your dev instance.


Thanks for your response. The scoped application doesn't seem to be a problem as I can access the URL through same credentials through SOAP UI or simple browser.  




Http Client is a good option. I will try this and get back to you asap.


Bryan Tay3
Mega Guru

hi Vivek,



I tried construct your java code using jersey 1.19 client and i can simulate the problem on my instance.


Anyway, you can try the following which works for me with 1.19 jersey client.



Client restclient = Client.create();                


restclient.addFilter(new HTTPBasicAuthFilter("user_name", "password"));


String URL = "URL";


WebResource webResource = restclient.resource(URL);


ClientResponse response = webResource.accept("application/json").get(ClientResponse.class);



Hope this helps.


Thanks Bryan. Trying this and will let you know the result asap.