Dear community,
im actually writing an exporter for all project issues in java using cookie authentication.
In the end, i want to safe all json data to a file i did get.
Problem: I do constantly receive http 401 as i use my session id (received from basic auth. before) as my cookie variable.
I did make sure that the syntax of my cookie is correct, so there have to be another detail i have missed. So my jsonData at the end of code still remains empty. Is another authentication needed?
I have full access to the project and everything else works fine.
<...> placeholder
output:
loginResponse:
{"session":{"name":"JSESSIONID","value":"3CA6985D902FEEB7FA5A79FF6E570218"},"loginInfo":{"failedLoginCount":6,"loginCount":130,"lastFailedLoginTime":"2019-09-16T15:02:49.185+0200","previousLoginTime":"2019-12-06T09:17:35.689+0100"}}
JSessionID:
"3CA6985D902FEEB7FA5A79FF6E570218"
https://<baseurl>.com/jira/rest/api/2/search?jql=project="<projectname>"&username=<username>
401
jsonData:
SUCCES!
code:
public static String parseJSessionID(String input) {
String jSessionID = "";
try{
JsonElement parser = new JsonParser().parse(input);
Object obj = parser.getAsJsonObject();
JsonObject jsonObject = (JsonObject) obj;
JsonObject sessionJsonObj = (JsonObject) jsonObject.get("session");
jSessionID = (String) sessionJsonObj.get("value").toString();
} catch (Exception ex){
System.out.println("Error in parseJSessionID: " + ex.getLocalizedMessage());
jSessionID = "ERROR";
}
System.out.println("\nJSessionID: ");
System.out.println(jSessionID);
return jSessionID;
}
public static String getJsonData(String baseURL, String jSessionID, String loginUserName) {
String jsonData = "";
try {
URL url = new URL(baseURL + "api/2/search" + "?jql=project=\"<projectname>\"" + "&username=" + loginUserName);
System.out.println(url.toString());
System.out.println("\n");
String cookie = "JSESSIONID=" + jSessionID;
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Cookie", cookie);
System.out.println(conn.getResponseCode());
if(conn.getResponseCode() == 200) {
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String output = "";
while((output = br.readLine()) != null) {
jsonData += output;
}
conn.disconnect();
}
} catch (Exception ex) {
System.out.println("Error in getJsonData: " + ex.getLocalizedMessage());
jsonData = "ERROR";
}