我正在尝试构建一个 JAVA 客户端以 POST 到 RESTApi,但是,这样做时我收到错误“用户未通过身份验证”。
在浏览 API 服务文档时,我发现在调用 API 之前,我必须获取一个 Auth 令牌,就像它在步骤 1 中所说的那样,然后在任何后续调用 API 中使用该令牌。
文件是这样说的:
1)
sessionToken=$(curl -s -X POST -d '{"userName":"NAME", "password": "xxxxx"}' https://host:18641/security/public-rest/v1/authentication/login -H "Content-Type:application/json" -H "X-Requested-By:SDC" -c - | grep SSO | grep -o '\S*$')
2)
curl -X POST https://host:18641/jobrunner/rest/v1/job/681c449d-7c22-48d6-9532-2e6ef74971bc/start --header "Content-Type:application/json" --header "X-Requested-By:SDC" --header "X-SS-REST-CALL:true" --header "X-SS-User-Auth-Token:$sessionToken" -i
现在,我无法在 JAVA 中实现相同的目标。
我正在使用泽西图书馆,这是我到目前为止的代码,
public static String testUploadService(String httpURL, File filePath,String User,String Pass,Processing processing) throws Exception {
// Thread.sleep(500);
// local variables
ClientConfig clientConfig = null;
Client client = null;
WebTarget webTarget = null;
Invocation.Builder invocationBuilder = null;
Response response = null;
FileDataBodyPart fileDataBodyPart = null;
FormDataMultiPart formDataMultiPart = null;
int responseCode;
String responseMessageFromServer = null;
String responseString = null;
String name = User;
String password = Pass;
String authString = name + ":" + password;
String sdc="sdc";
byte[] encoding = Base64.getEncoder().encode(authString.getBytes());
byte[] encoding2 = Base64.getEncoder().encode(sdc.getBytes());
String USER_PASS = new String(encoding);
String auth2=new String(encoding2);
String boundary = "=-=" + System.currentTimeMillis() + "=-=";
// Thread.sleep(500);
try{
// invoke service after setting necessary parameters
ClientConfig cc = new ClientConfig();
cc.register(MultiPartFeature.class);
try {
client = new JerseywithSSL().initClient(cc);
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
webTarget = client.target(httpURL);
// set file upload values
fileDataBodyPart = new FileDataBodyPart("file", filePath, MediaType.APPLICATION_OCTET_STREAM_TYPE);
formDataMultiPart = new FormDataMultiPart();
formDataMultiPart.bodyPart(fileDataBodyPart);
invocationBuilder = webTarget.request();//.header("Authorization", "Basic " + authString);
invocationBuilder.header("Authorization", "Basic " + USER_PASS);
invocationBuilder.header("X-Requested-By","SDC");
invocationBuilder.header("Content-type", "multipart/form-data; boundary=" + boundary);
try{response = invocationBuilder.post(Entity.entity(formDataMultiPart, MediaType.MULTIPART_FORM_DATA));}
catch(Exception ex){
ex.printStackTrace();
}
responseCode = response.getStatus();
System.out.println("Response code: " + responseCode);
if (response.getStatus() != 200) {
// throw new RuntimeException("Failed with HTTP error code : " + responseCode);
}
System.out.println("Check 6");
// get response message
responseMessageFromServer =
response.getStatusInfo().getReasonPhrase();
System.out.println("ResponseMessageFromServer: " +
responseMessageFromServer);
System.out.println("Check 7");
processing.setlabel("Finished");
processing.setprogress(100);
// get response string
responseString = response.readEntity(String.class);
processing.finished("Server Response Code - "+responseCode + "\n ResponseMessageFromServer: "+ responseString);
}
catch(Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "Error!! \n Make sure you are connected to Dell Internal Network");
processing.dispose();
}
finally{
// release resources, if any
fileDataBodyPart.cleanup();
formDataMultiPart.cleanup();
formDataMultiPart.close();
response.close();
client.close();
}
return responseString;
}
我需要帮助来获取 Auth Token,就像在第一个 Curl 命令中所做的那样,我可以创建第二个 Post 命令。