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-18-2019 11:25 PM
Hi,
This error means incorrect username and password. check the user credentials and do once again.
Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2019 10:42 PM
Thank you for replying.
However, I checked username and password combo many times. I checked that username and password are correct.
So, I'd like to know other cause of this Problem.
Thank you
Norihiko
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2019 11:35 PM
Hi,
Ok so username and password is correct. Does that user has access to create/update record on table
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2019 10:37 PM
Hi,
Any update on this?
Can you mark answer as correct, helpful if you were able to achieve the requirement. This helps in removing this question from unanswered list and helps users to learn from your thread. Thanks in advance.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader