'Create Confluence page using Python and Atlassian API

I try to create a Confluence page with Python by:

from atlassian import Confluence  

def writeConfluencePage(confluenceBaseUrl,parentId,user,password):  

    # connect to Confluence
    confluence = Confluence(url=confluenceBaseUrl,username=user,password=password)
    
    # create page
    status = confluence.create_page(  
    space='TEST', 
    parent_id=parentId,
    title='Made by Python',  
    body='This is the body')
      
    print(status) 

With this I get the following error:

requests.exceptions.HTTPError: Could not create content with type page

The parentId definitely exists. The space not, but I think I have to create something, do I? I also have definitely write access to the parent page.

What is wrong here?



Solution 1:[1]

If you look at the source code of the function create_page:

data = {
    "type": type,
    "title": title,
    "space": {"key": space},
    "body": self._create_body(body, representation),
}

so in space you must specify the key, not the name

from atlassian import Confluence  

def writeConfluencePage(confluenceBaseUrl,parentId,user,password):  

    # connect to Confluence
    confluence = Confluence(url=confluenceBaseUrl,username=user,password=password)  
    
    # get space
    content = confluence.get_page_by_id(page_id=parentId)
    space = content["space"].get("key")

    # create page
    status = confluence.create_page(  
    space=space, 
    parent_id=parentId,
    title='Made by Python',  
    body='This is the body')
      
    print(status) 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yaroslav752