I used the following API to get issue details:
https://eanand1886.atlassian.net/rest/api/2/issue/10000
which provides the following attachment URL:
https://eanand1886.atlassian.net/secure/attachment/10000/failure-smiley.png
Code that i tried:
public static void makeHttpGet(){ CloseableHttpClient httpclient = HttpClients.createDefault(); try{ String url = "https://eanand1886.atlassian.net/secure/attachment/10000/failure-smiley.png"; String accessToken = "***********************"; HttpGet get = new HttpGet(url); get.setHeader("oauth_signature", CONSUMER_PRIVATE_KEY); get.setHeader("oauth_token", accessToken); get.setHeader("oauth_consumer_key", "********"); get.setHeader("oauth_signature_method", "RSA-SHA1"); get.setHeader("oauth_timestamp", ""+System.currentTimeMillis()); get.setHeader("oauth_nonce", "abcd"+System.currentTimeMillis()); get.setHeader("oauth_version", "1.0"); System.out.println("URL:"+url); CloseableHttpResponse response = httpclient.execute(get); int status = response.getStatusLine().getStatusCode(); System.out.println("code:"+status); System.out.println(response); if (status >=200 && status < 300) { HttpEntity entity = response.getEntity(); if (entity.isStreaming()) { System.out.println("Streaming..."); byte data[] = EntityUtils.toByteArray(entity); FileOutputStream fout = new FileOutputStream(new File("failure.png")); fout.write(data); fout.close(); System.out.println("Done!!"); } } }catch(Exception e) { e.printStackTrace(); } }
The above code downloads a file with the login page content i.e. it redirects to login page instead of downloading the actual file.
This works, check it out
@GetMapping(value = "/getAttachment")
public String getAttachment(@RequestParam("id") String attachmentID) throws Exception {
System.out.println("Entered");
Client client = Client.create();
WebResource webResource = client.resource(jiraBaseURLRest + "attachment/" +
attachmentID);
ClientResponse response = webResource.header("Authorization", "Basic " +
base64Creds).type("application/json")
.accept("application/json").get(ClientResponse.class);
String result = response.getEntity(String.class);
JSONObject jsonObj = new JSONObject(result);
System.out.println("JSON Object = "+jsonObj);
URL url = new URL(jsonObj.getString("content"));
Closeable httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url.toURI());
httpGet.setHeader("Authorization", "Basic " + base64Creds);
CloseableHttpResponse response1 = ((CloseableHttpClient) httpclient).execute(httpGet);
if(response1.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response1.getEntity();
if (entity.isStreaming())
{
System.out.println("Streaming...");
byte data[] = EntityUtils.toByteArray(entity);
FileOutputStream fout = new FileOutputStream(new File("D://pdf1.pdf"));
fout.write(data);
fout.close();
System.out.println("Done!!");
}
}
return "Success";
}
I am having the same issue, but with basic authentication. How are you connecting with basic auth and downloading the file?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Kevin,
Try the following code for basic authetication
public static void makeBasicAuth(){ try{ HttpClient hc = new HttpClient(); String url = "https://domain.atlassian.net/secure/attachment/10000/file.png"; GetMethod get = new GetMethod(url); BASE64Encoder base64encoder = new BASE64Encoder(); byte[] b = "username:password".getBytes(); String cp = base64encoder.encode(b); get.setRequestHeader("Authorization", "Basic "+cp); int code = hc.executeMethod(get); System.out.println("Code:"+code); InputStream is = get.getResponseBodyAsStream(); if(is!=null && code >=200 && code <= 300){ FileOutputStream fout = new FileOutputStream(new File("file.png")); int read = 0; byte[] bytes = new byte[1024]; while ((read = is.read(bytes)) != -1) { fout.write(bytes, 0, read); } }else{ System.out.println("Download failed"); } }catch(Exception e){ e.printStackTrace(); } }
-Anand
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.