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

Hi Bryan


Your suggestion worked for me. It turns out that in my code encoded username and password was not getting accepted by SN instance and hence 401 error. As soon as I removed the encoding the credentials worked. Thanks a lot for your help on it.