How can I create a new page to confluence with Python

前端 未结 3 1959
没有蜡笔的小新
没有蜡笔的小新 2021-01-06 09:12

I am trying to create a new page into confluence using Python\'s xmlrpclib. I already know how to update content of an existing page, but how can I create a tot

3条回答
  •  时光说笑
    2021-01-06 09:58

    Answer of "J. Antunes" is correct, but it took a long time for me to find the APIs, pageIds etc. This answer will give you a step by step guide on how to achieve this with API Tokens.

    Step 1: Generate API Token in Confluence.

    In the confluence page, go to Settings and click on password. Click on "Create and manage API tokens" and get the token. {TOKEN}

    Step 2: Find the parent page you want to create a child page at

    Go to get the parent page ID, and find the {Parent Page ID}

    Step 3: Get the Space Key

    On confluence, go to Space Settings, and find the Space Key mentioned.{SPACE KEY}

    Step 4: Now get started with the code

    import requests
    import json
    from requests.auth import HTTPBasicAuth
    
    # set auth token and get the basic auth code
    auth_token = "{TOKEN}"
    basic_auth = HTTPBasicAuth('{email you use to log in}', auth_token)
    
    # Set the title and content of the page to create
    page_title = 'My New Page'
    page_html = '

    This page was created with Python!

    ' parent_page_id = {Parent Page ID} space_key = '{SPACE KEY}' # get the confluence home page url for your organization {confluence_home_page} url = '{confluence_home_page}/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', } } } # We're ready to call the api 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))

提交回复
热议问题