View Confluence Page Images via API

Daniel_Holmes
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
April 8, 2019

We are currently using the Confluence Cloud API to retrieve pages and display them to users logged in to our website. We can retrieve and display all the content without a problem, except that the images aren't being displayed. I understand that this is because an active (logged in) Confluence session is required to be able to view the images.

Our users logged into our website do not have Confluence accounts. Is there a way for these users to view the images without having to be logged in to Confluence? Is it at all possible using the API?

1 answer

1 accepted

0 votes
Answer accepted
Stephen Sifers
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
April 16, 2019

Hello Daniel,

Ideally, you’re going to want users to access Confluence to see the content (either being authenticated or anonymous). There are workarounds, but they’re not always supported when something breaks or changes.

The workaround would be to use another endpoint to get the contents of the attachment themselves and then display this content. You may have to add an additional step to store the attachment external to Confluence to allow viewing. Doing this will require the user of the following endpoint:

GET /wiki/rest/api/content/{id}/child/attachment

As stated before, this may not be a permanent solution due to possible future changes within the API or the products.

Regards,
Stephen Sifers

Connor Bär
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
May 11, 2020

Hello Daniel & Stephen,

 

My team is currently working on a similar project: we're building a custom frontend for public documentation pages that are stored in Confluence.

I've played around with the API endpoint that Stephen suggested (GET /wiki/rest/api/content/{id}/child/attachment), but it only seems to return JSON metadata, not the file content itself. Am I missing something here? Could you share a sample query to fetch the file itself?

I've also tried to reverse engineer Atlassian's asset API (api.media.atlassian.com), but couldn't figure out how to successfully authenticate with it from the server.

Our plan is to proxy asset requests from our frontend through our server (which handles authentication) to Atlassian. This way, we can ensure that the assets remain secure by default while making some of them accessible to the public.

 

Any tips and hints are much appreciated!

Like BI Automate likes this
Daniel_Holmes
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
May 11, 2020

Hi Connor,

You will need to do an additional request to download the file. You can find the file links in the JSON metadata in the response from GET /wiki/rest/api/content/{id}/child/attachment. Below is an implementation in Python using the requests library. Let me know if it helps:

attachments_url = f"https://yoursite.atlassian.net/wiki/rest/api/content/{id}/child/attachment"
auth = (CONFLUENCE_API_USER, CONFLUENCE_API_KEY)
response = requests.get(attachments_url, auth=auth)
data = response.json()
file_data = data['results']

for file in file_data:
download_url = "https://yoursite.atlassian.net/wiki" + file['_links']['download']
response = requests.get(download_url, auth=auth)
image_file = ContentFile(response.content)
Like Connor Bär likes this
MrHankey
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
November 17, 2021

Hi @Daniel_Holmes 

thank you for your code snippet. I just wanted to ask you the following question:

What happens to the image_file? I mean... sure you got the file but then ..? I could also consider saving those ContentFiles in an array and passing it to the frontend. But how can we achieve to combine the page content with the images, without making the (unrecommended) workaround Stephen already suggested?

Thank you in advance!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events