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.
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.
what is page_data_json?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
In case you are using the Python SDK to authenticate against Confluence using a Personal Access Token (PAT), the example above did not work for me, since somehow the urllib3 HTTPConnectionPool "forgets" about the "Bearer" Token, but uses a "Basic" Auth token, which is wrong.
The hack to circumvent is, is to introduce your own HTTP Adapter:
import os
import requests
from atlassian import Confluence
from requests.adapters import HTTPAdapter
class CustomHTTPAdapter(HTTPAdapter):
def __init__(self, headers=None, *args, **kwargs):
self.default_headers = Confluence.default_headers
self.default_headers.update(headers)
super().__init__(*args, **kwargs)
def send(self, request, **kwargs):
# Merge default headers with request-specific headers
request.headers.update(self.default_headers)
return super().send(request, **kwargs)
session = requests.Session()
adapter = CustomHTTPAdapter(headers={"Authorization": f"Bearer {os.environ["CONFLUENCE_TOKEN"]}"})
session.mount(os.environ["CONFLUENCE_HOST"], adapter)
confluence = Confluence(session=session, url=os.environ["CONFLUENCE_HOST"])
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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>>")
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.