Create a page in Confluence - Python API

Marlene Pirker May 21, 2021

Hello! 

I am trying to create a page in Confluence with the python API. I tried it like written here: 

https://stackoverflow.com/questions/33168060/how-can-i-create-a-new-page-to-confluence-with-python?newreg=1c54fe53dece4bc7b8d7acfc72629651

This is my code:

from atlassian import Confluence
import requests
import json
from requests.auth import HTTPBasicAuth

confluence = Confluence(
url='..',
username='...`, 
password='...')

page_title = 'My New Page'
page_html = '<p>This page was created with Python!</p>'

parent_page_id = ...
space_key = '...'

auth_token = ".." 
basic_auth = HTTPBasicAuth('mail', auth_token)

 

url = 'https://.../rest/api/content/'

# Request Headers
headers = {
'Content-Type': 'application/json;charset=iso-8859-1',
}

# Request body
data = {
'type': 'page',
'title': page_title,
'ancestors': [{'id':parent_page_id}],
'space': {'key':space_key},
'body': {
'storage':{
'value': page_html,
'representation':'storage',
}
}
}

 

try:

r = requests.post(url=url, data=json.dumps(data), headers=headers, auth=basic_auth)

# Consider any status other than 2xx an error
if not r.status_code // 100 == 2:
print("Error: Unexpected response {}".format(r))
else:
print('Page Created!')

except requests.exceptions.RequestException as e:

# A serious problem happened, like an SSLError or InvalidURL
print("Error: {}".format(e))

 

my first question: where do I get the token? With this token https://id.atlassian.com/manage-profile/security/api-tokens it doesn't work. 

second the output is: 

Unexpected response <Response [401]>

I think the authentication doesn't work. But i dont know how to authenticate....  

 

If I try this: 

oauth_dict = {
    'access_token': 'access_token',
    'access_token_secret': 'access_token_secret',
    'consumer_key': 'consumer_key',
    'key_cert': 'key_cert'}

https://atlassian-python-api.readthedocs.io/index.html

Where do i get the secret token, the consumer key and the key_cert?? 

pls help.. 

2 answers

1 accepted

1 vote
Answer accepted
Dario B
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 9, 2021

Hello @Marlene Pirker ,

Please notice that the Atlassian Python API is neither built nor supported by Atlassian and there are chances this is a bug with this 3rd party API.

For the rest, as mentioned in the Basic auth for REST APIs documentation page, you need to use the same email address you use to log-into Confluence Cloud as the username and an  API token (that you indeed generate from https://id.atlassian.com/manage-profile/security/api-tokens) as the password.

Please review the below links for further details on this:

 

I hope this helps.

 

Cheers,
Dario

Marlene Pirker June 10, 2021

Thank you so much for your answer. I already managed to connect with Confluence Cloud using the API and a token. I am not able to connect to the server but that seems to be a permission problem from our side.

Dario B
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 10, 2021

@Marlene Pirker You are very welcome and I am happy to know this helped :) 

Also, please notice that in order to authenticate REST API calls against Jira Server you need to use username and password instead of email address and api token.

I hope this helps. 

Like Konstantin Weber - SVA likes this
0 votes
Dirk Faulhaber July 16, 2023

To make https://github.com/atlassian-api/atlassian-python-api work with a free atlassian instance and an api key I had to follow this hack:

user = 'myuser@googlemail.com'
apikey = 'COPIED_APITOKEN'
serverUrl = 'https://myuser.atlassian.net/'

auth_key = base64.b64encode(bytes(user + ":" + apikey,"utf-8")).decode("ascii")

confluence = Confluence(url=serverUrl)
confluence._update_header("Authorization", "Basic " + auth_key)


spaces = confluence.get_all_spaces()

this was done following https://developer.atlassian.com/cloud/confluence/basic-auth-for-rest-apis/.

This worked for

atlassian-python-api 3.39.0

All other efforts I tried to make basic auth work, failed.

Suggest an answer

Log in or Sign up to answer