0

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

How can I create a new page to confluence with Python

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 Answers2

0

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....

I believe I can answer this part of the question. If I understand it correctly you paste your token into the password field in the part below.

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

If using a Personal Access Token (PAT) then you need to change password -> token. See below:

 confluence = Confluence(
    url='..',
    username='...', 
    token='PAT goes here')
Mange_Man
  • 41
  • 1
  • 6
  • Create an API token here https://id.atlassian.com/manage-profile/security/api-tokens Then use this in combination with the code I shared above so that you have a concatenated base64 string – James Stanbridge Jun 24 '23 at 15:26
0

I wonder if the issue you are seeing is that the API requirements are now that you must pass the authorization string as base64.

I have this function within my Confluence Class (I used a class, so self.username, self.password and self.api_token are already declared)

def auth_create(self):
    """Creates an authorization string as per the atlassian API requirements

    Returns:
        str: authorization string
    """
    import base64
    api_token = self.api_token

    # Construct the string
    auth_string = self.username + ":" + api_token

    # Encode the string in base64 format
    auth_token = base64.b64encode(auth_string.encode()).decode()
    logging.debug(f"username: {self.username}")
    return auth_token