我正在尝试使用Java应用程序来调用http POST方法,以便访问ServiceDesk Plus REST API,如其API文档中所述:http://www.manageengine.com/products/service-desk/help/adminguide/index.html。
我遵循位于API / REST API /请求操作/
的本文档中的“添加请求”格式运行java应用程序时收到以下错误消息:“收到的API密钥未与任何技术人员关联。身份验证失败。”
我没有管理员权限来生成API密钥,但我的老板给了我一个。我确信它没有过期或类似的东西。出于隐私原因,我没有提供真正的密钥。
这是我第一次尝试使用任何API。我有java和面向对象编程的基本教科书知识,但我使用的大部分代码都被复制了。我对它使用的库和方法一点都不熟悉,所以如果我做了一些愚蠢的话我会道歉。任何帮助将不胜感激:)
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());
}
}
答案 0 :(得分:0)
在联系ServiceDesk Plus后,事实证明技术人员密钥与我没有正确关联,如上所述的错误消息。这根本不是Java问题。对不起浪费任何人的时间。