I am attempting to use a Java application to call an http POST method in order to access the ServiceDesk Plus REST API as explained in their API documentation:http://www.manageengine.com/products/service-desk/help/adminguide/index.html.
I followed the format for "Adding Request" in this documentation located at API/REST API/Request Operations/
I am getting the following error message when running the java application: "API key received is not associated to any technician. Authentication failed."
I do not have admin rights to generate an API key, but I was given one by my boss. I am sure it is not expired or anything like that. For privacy reasons, I have not included the real key.
This is the first time I have tried to use any API. I have basic textbook knowledge of java and object-oriented programming, but most of the code I used was copied. I am not at all familiar with the libraries and methods it uses, so I apologize if I am doing something dumb. Any help would be greatly appreciated :)
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
public class HttpURLConnectionExample {
private final String USER_AGENT = "Mozilla/5.0";
public static void main(String[] args) throws Exception {
HttpURLConnectionExample http = new HttpURLConnectionExample();
System.out.println("\nTesting 1 - Send Http POST request");
http.sendPost();
}
// HTTP POST request
private void sendPost() throws Exception {
String url = "https://sd.centerednetworks.com/sdpapi/request";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
//add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
//define parameters
String operation = "OPERATION_NAME=ADD_REQUEST";
String techkey = "&TECHNICIAN_KEY=XXXXXXXXXXXXXXXXXXX";
String data = "&INPUT_DATA=<Operation><Details><parameter><name>requester</name> <value>Shawn Adams</value></parameter>"
+ "<parameter><name>subject</name><value>The subject of the request</value></parameter>"
+ "<parameter><name>description</name><value>The description of the request</value></parameter>"
+ "<parameter><name>callbackURL</name><value>http://localhost:8080/CustomReportHandler.do</value></parameter>"
+ "<parameter><name>requesttemplate</name><value>Unable to browse</value></parameter>"
+ "<parameter><name>priority</name><value>High</value></parameter>"
+ "<parameter><name>site</name><value>New York</value></parameter>"
+ "<parameter><name>group</name><value>Network</value></parameter>"
+ "<parameter><name>technician</name><value>Jeff Hein</value></parameter>"
+ "<parameter><name>level</name><value>Tier 3</value></parameter>"
+ "<parameter><name>status</name><value>Open</value></parameter>"
+ "<parameter><name>service</name><value>Email</value></parameter> </Details></Operation>";
//String urlParameters = operation + techkey + data;
String urlParameters = operation + techkey + data;
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
}
}