When Servicenow API is called from java, the error message show not 401 Unauthorized.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2019 11:22 PM
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
- Labels:
-
Personal Developer Instance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2019 11:37 PM