I have a standalone C# desktop application which gets the body of a Confluence page via an API call (using Basic authentication).
Where there is an image (attachment) for the page, I pick up the _links.download URL which is used to download the attachment. I'm struggling with knowing how to authenticate this download, because it isn't a REST API call, but just a URL.
Does anyone have sample code (preferably C# or other .Net, but I'll try any language), which shows how to successfully download a Confluence attachment?
The code below is what I'm trying, but without authentication it gets garbage which isn't recognised as an image file
string imageUrl = "https://coname.atlassian.net/wiki" + attachment._links.download;
string saveLocation = @D:\ + attachment.title;
byte[] imageBytes;
HttpWebRequest imageRequest = (HttpWebRequest)WebRequest.Create(imageUrl);
WebResponse imageResponse = imageRequest.GetResponse();
Stream responseStream = imageResponse.GetResponseStream();
using (BinaryReader br = new BinaryReader(responseStream))
{
imageBytes = br.ReadBytes(500000);
br.Close();
}
responseStream.Close();
imageResponse.Close();
File.WriteAllBytes(saveLocation, imageBytes);