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.
However it works when i trigger a request with basic authentication(username:pass) in header, instead of Oauth.
-Anand