1

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());

  }
}
jhein
  • 11
  • 1
  • 2
  • Can you post the stacktrace of the error ?! – Jorge Campos Jul 10 '14 at 00:12
  • You miss some few thing on your post. look at here: http://stackoverflow.com/a/4206094/460557 – Jorge Campos Jul 10 '14 at 00:19
  • I tried adding the setRequestProperty lines that I was missing from that link. Same error. This is the output that the function returned to the console: "Response Code : 200 FailedAPI key received is not associated to any technician. Authentication failed.". Is that what you mean? I don't know what I'm doing. @Jorge Campos – jhein Jul 10 '14 at 19:52
  • Edit your question with the modifications you did so we can see – Jorge Campos Jul 10 '14 at 22:13

1 Answers1

0

After contacting ServiceDesk Plus, it turns out the technician key wasn't properly associated to me, as the error message stated. This wasn't a Java issue at all. Sorry for wasting anyone's time.

jhein
  • 11
  • 1
  • 2
  • I am having same requirement and I am new to this could you please help me understanding How to fetch requests using API serviceDeskPlus – John D Oct 06 '17 at 13:15