Unauthorized while linking changeset from SCM to JIRA using REST API
Hello Team,
public Boolean createLink(String jiraUrl) {
#####
String linkEndpoint = fTeamRepository.getRepositoryURI()
+ "service/com.ibm.team.scm.common.internal.rest.IScmRestService2/addChangeRequests";
String responseBody = "changeSetItemId=" + fChangeSet.getItemId().getUuidValue() + "&targetOslcResourceUris="
+ URLEncoder.encode(jiraUrl) + "&targetLabels=" + "Test%20scm%20to%20jira%20linking"
+ "&targetRepoIds=&targetRepositoryURIs=";
HttpURLConnection linkConn = (HttpURLConnection) new URL(linkEndpoint).openConnection();
linkConn.setRequestMethod("POST");
byte[] decodedBytes = Base64.getDecoder().decode(encodedKey.trim());
String pswd = new String(decodedBytes);
String auth = fTeamRepository.loggedInContributor().getUserId() + ":" + pswd;
String encodedAuth = Base64.getEncoder().encodeToString((auth).getBytes());
linkConn.setDoOutput(true);
linkConn.setInstanceFollowRedirects(true);
linkConn.setRequestProperty("Authorization", "Basic " + encodedAuth);
linkConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
linkConn.setRequestProperty("accept", "text/json");
linkConn.setRequestProperty("x-requested-with", "XMLHttpRequest");
linkConn.setRequestProperty("X-Jazz-CSRF-Prevent", "1");
//linkConn.setRequestProperty("Cookie", cookies);
try (OutputStream os = linkConn.getOutputStream()) {
byte[] input = responseBody.getBytes("utf-8");
os.write(input, 0, input.length);
}
int responseCode = linkConn.getResponseCode();
String responseMsg = linkConn.getResponseMessage();
System.out.println("Response: " + responseCode + " " + responseMsg);
####
}
One answer
Comments
Hello Ralph, thank you for your prompt answer. The script is working fine now on Jenkins trigger for daily even after I have hardcoded value of JSESSIONID in the header.
You get the JSESSIONID cookie during the login process. You are NOT suggested to invent an arbitrary valuer and put it there. This might bite you in the future.