How to access Confluence use atlassian-python-api and a Personal Access Token

nirabe October 20, 2023

I can use the following for basic auth:

from atlassian import Confluence

confluence = Confluence(url='https://internalconfluenceurl.com', username = 'myusername', password='mypassword')

This also works if I use an API token value instead of password.

How do I alter this to use a Personal Access Token (as created within Confluence) instead? I tried substituting that in for the password, but it gives an error.

Is there a further authentication step that I am missing? I have managed to do something similar in Jira, but the same approach does not work for Confluence. 

3 answers

1 accepted

2 votes
Answer accepted
nirabe October 20, 2023

I found a different approach:

import requests

response = requests.post(base_url, data=page_data_json, headers={'Authorization': f'Bearer {pat}',"Accept": "application/json",'Content-Type': 'application/json'})

if response.status_code == 200:
print("Child page created successfully")
else:
print("Failed to create the child page. Status code:", response.status_code)

It is not quite as convenient as using the inbuilt Confluence function, but it seems to work. 

Akshayaa Pandiyan
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!
January 9, 2025

what is page_data_json?

0 votes
GMK
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 9, 2024

Hello All, 

First of all it is worth noting  atlassian-python-api   is an open source project, it is not owned/maintained by Atlassian. 

You can use personal assess token with this library, the only difference is in syntax between cloud and data-center:

 

from atlassian import Jira
# To be used with Jira cloud
jira_cloud = Jira(url="<url>", username="username", password="password")

# Use with data-center
jira_dc = Jira(url="url", token="<token>>")

 

0 votes
Sagar
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 20, 2023

Hi @nirabe Welcome to the Atlassian community.

Have you check the below document for how to use the PAT in Jira/Confluence instead of basic auth?

https://confluence.atlassian.com/enterprise/using-personal-access-tokens-1026032365.html

Below is the example of using PAT same way you need to modify to use in Python script

 

curl -H "Authorization: Bearer <yourToken>" https://{baseUrlOfYourInstance}/rest/api/content
nirabe October 20, 2023

Hi @Sagar and thanks for the prompt reply.

Yes, I had seen the link you shared. I cannot use cURL. There have been some suggestions of using requests instead.

I may go down this route, but it seems much more complicated than the approach for Jira, which is odd.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events