When Servicenow API is called from java, the error message show not 401 Unauthorized.

norihiko
Tera Contributor

Hi
I'd like to create incident ticket in servicenow from java code by using Servicenow Table API.
I made java code. However, 401 Unauthorized message is showed.
Basic authentication is used. The user has role rest_service, soap, web_service_admin, itil.     I'd like to know way how to solve this problem.


---java code---
import java.io.IOException;

import org.apache.http.HttpEntity;
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.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
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;

public class IF_TEST{
public static void main(String[] args) throws IOException, HttpException {
IF_TEST restAction = new IF_TEST();
restAction.postRequest();
}

public void postRequest() throws HttpException, IOException {
// This must be valid json string with valid fields and values from table
String postData = "{\"short_description\":\"Test with java post\"}";
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(new HttpHost("https://devxxxxx.service-now.com")),
new UsernamePasswordCredentials("userid", "password"));
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.build();

try {
HttpPost httpPost = new HttpPost("https://devxxxxx.service-now.com/api/now/table/incident");
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-Type", "application/json");

HttpEntity entity = new ByteArrayEntity(postData.getBytes("utf-8"));
httpPost.setEntity(entity);

System.out.println("Executing request " + httpPost.getRequestLine());
CloseableHttpResponse response = httpclient.execute(httpPost);
try {
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
String responseBody = EntityUtils.toString(response.getEntity());
System.out.println(responseBody);
} finally {
response.close();
}
} finally {
httpclient.close();
}
}

}
-------------------------

---Error message-----
Executing request POST https://devxxxxx.service-now.com/api/now/table/incident HTTP/1.1
----------------------------------------
HTTP/1.1 401 Unauthorized
{"error":{"message":"User Not Authenticated","detail":"Required to provide Auth information"},"status":"failure"}


----------------------

Thank you.
Norihiko

 

5 REPLIES 5

ARG645
Tera Guru
Is there any way to log the headers and their values you are sending in the request in your code ? If yes, then check for the Authorization Header and it’s value, it’s usually a base64 encode if user name and password . If your request is missing the Authorization Header or if it’s sending a wrong value, then most probably that is the cause of your error.