I am trying to use a curl script that works perfectly fine on both Windows and Linux in a Java program. As it is a very specific use-case, it needs to be taken from a text file and may be changed on the fly. So my goal is to create a java class that can just complete a generic curl call to a specific website using the Runtime.getRuntime().exec function.
When the code is run, it gives back "{"error":"unauthorized","error_description":"An Authentication object was not found in the SecurityContext"}". I have tried swapping single quotes for double quotes, as well as removing quotes all-together (as one answer on here said worked for them). None of those things worked. Some of them appeared to be causing the code to assume it was multiple curl calls in one and said it could not find hosts for each line in the string.
Before I go ahead and re-write everything to parse the string into the ProcessBuilder, is there something I have missed that would solve this?
The String I have been using
String tokenResults = runProcess("curl -X POST https://fakeurl/oauth/token -H \"Authorization: "
+ "Basic fakestringwithabunchofrandomcharacters1234567890123=\" "
+ "-H \"Content-Type: application/x-www-form-urlencoded\" -d grant_type=client_credentials");
The code for runProcess:
private String runProcess(String command) throws SpecialException{
try {
Process proc = Runtime.getRuntime().exec(command);
InputStream resultStream = proc.getInputStream();
InputStream errorStream = proc.getErrorStream();
// If there was an error creating this process, print it and fail out
if(errorStream.available() > 0) {
BufferedReader errors = new BufferedReader(new InputStreamReader(errorStream));
StringBuilder errorString = new StringBuilder();
String line;
while( (line = errors.readLine()) != null) {
errorString.append(line);
}
throw new SpecialException(this.getClass().getName(), "Failed to complete the process due to the following error:\n"
+ errorString.toString());
}
// Since there were no errors, read the output
BufferedReader results = new BufferedReader(new InputStreamReader(resultStream));
StringBuilder resultString = new StringBuilder();
String line;
while( (line = results.readLine()) != null) {
resultString.append(line);
}
return resultString.toString();
} catch (IOException e) {
log("Failed to complete the process. Please check your input and try again");
throw new SpecialException(this.getClass().getName(), "Failed to complete the process being run", e);
}
}