Skip to content

Using API Tokens To Download Attachments


If you disable Basic Authentication with passwords in the System-Wide settings, you also can't authenticate on non REST endpoints with API Tokens directly.

This is because of technical reasons and required for security, to make sure nobody can cheat their way around (getting a session with a password on a non-REST endpoint and using that on the REST API and evade token scope - or other restrictions).
So if you need to download attachments, which are not served on REST endpoints, get a session from a REST endpoint first and use that to download.


In order to download attachments, one needs to authenticate against a REST endpoint first and use the session cookie for the subsequent request to retrieve the attachment via the non-REST endpoint.
Below are some Curl examples for Jira and Confluence.

Jira

Python

Please use version 1.8.1 if you want to use the Python Jira library

The Jira Python library provides some convenient methods to retrieve Jira content. Please see https://pypi.org/project/jira/ for installation instructions.
The following snippet shows how to authenticate with a user and API token and how to download attachments from an issue.

  1. from jira import JIRA
  2.  
  3. username = "someuser"
  4. apitoken = "sometoken"
  5. download_folder = "/path/to/download/folder/"
  6. server =  "https://<jira-base-url/"
  7.  
  8. jira = JIRA(basic_auth=(username, apitoken), options={'server': server})
  9. issue = jira.issue(<issue-key>, fields='summary,comment,attachment')
  10.  
  11. for attachment in issue.fields.attachment:
  12.     with open(download_folder + '%s' % (attachment.filename), 'wb') as file:
  13.         file.write(attachment.get())

Curl

Jira provides details about issue attachments on the following endpoint. The response JSON can be parsed to get the download URL.

  1. https://<jira-url>/rest/api/2/issue/<issueIdOrKey>?fields=attachment
image2021-2-19_11-10-56.png


List attachments, get session from API Token authentication at the same time

The following call lists all attachment details for a given issue in Jira and saves the session cookie. You need to parse the JSON output to get the attachment download URL.

  1. curl -c /path/to/some/folder/cookie-jar.txt -u username:apitoken "https://<jira-url>/rest/api/2/issue/<issueIdOrKey>?fields=attachment"

The below call shows how to use the session cookie again to download the file, using the download URL retrieved after parsing the response with the call before.

  1. curl -b /path/to/some/folder/cookie-jar.txt "https://<jira-url>/secure/attachment/10100/portallog.jpg" --output /path/to/some/folder/portallog.jpg

Confluence

Curl

Confluence provides details about page attachments on the following endpoint. The response JSON can be parsed to get the download URL.

  1. https://confluence-url/rest/api/content/<page-id>/child/attachment
image2021-2-19_11-23-8.png

List attachments for a page, get session from API Token authentication at the same time

The following call lists all attachment details for a given page id in Confluence and saves the session cookie. You need to parse the JSON output to get the attachment download URL.

  1. curl -c /path/to/some/folder/cookie-jar.txt -u username https://confluence-url/rest/api/content/<page-id>/child/attachment

The below call shows how to use the session cookie again to download the file

  1. curl -b /path/to/some/folder/cookie-jar.txt "https://confluence-url/download/attachments/1212445/sample.xlsx" --output /path/to/some/folder/sample.xlsx