如何使用Python在Confluence上创建新页面

8

我正在尝试使用Python的xmlrpclib创建一个Confluence新页面。我已经知道如何更新现有页面的内容,但是如何创建一个全新的页面呢?

我使用以下脚本来更新内容:

import xmlrpclib

CONFLUENCE_URL='https://wiki.*ownURL*/rpc/xmlrpc'

def update_confluence(user, pwd, pageid, newcontent):    
    client = xmlrpclib.Server(CONFLUENCE_URL,verbose=0)      
    authToken=client.confluence2.login(user,pwd)
    page = client.confluence2.getPage(authToken, pageid)
    page['content'] = newcontent
    cient.confluence2.storePage(authToken, page)
    client.confluence2.logout(authToken)

这种方式在更新内容时效果很好。但是问题在于,当我创建一个新页面时,需要解决pageID的问题,而我不知道如何做到这一点。

还有其他方法可以创建新页面吗?

3个回答

11
你可以使用Confluence REST API创建页面: https://docs.atlassian.com/atlassian-confluence/REST/latest-server/ 这里是一个 Python3 的示例。你需要知道父页面 ID。
import requests
import json
import base64

# Set the confluence User and Password for authentication
user = 'USER'
password = 'PASSWORD'

# Set the title and content of the page to create
page_title = 'My New Page'
page_html = '<p>This page was created with Python!</p>'

# You need to know the parent page id and space key.
# You can use the /content API to search for these values.
# Parent Page example http://example.com/display/ABC/Cheese
# Search example: http://example.com/rest/api/content?title=Cheese
parent_page_id = 123456789
space_key = 'ABC'

# Request URL - API for creating a new page as a child of another page
url = 'http://example.com/rest/api/content/'

# Create the basic auth for use in the authentication header
auth = base64.b64encode(b'{}:{}'.format(user, password))

# Request Headers
headers = {
    'Authorization': 'Basic {}'.format(auth),
    'Content-Type': 'application/json',
}

# 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)

    # 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))

很遗憾,这在Atlassian Cloud上无法工作。 - Nico Müller

9

"J. Antunes" 的答案是正确的,但我花了很长时间才找到API、页面ID等。本回答将为您提供一步一步的指南,使用 API Token实现此目标。

第1步:在Confluence中生成API Token。

在Confluence页面上,前往“设置”并单击“密码”。单击“创建和管理API Token”,获取令牌。{TOKEN}

enter image description here

第2步:找到要在其中创建子页面的父页面

前往获取父页面ID,并找到{Parent Page ID}

第3步:获取空间键

在Confluence上,前往空间设置,并查找所提及的空间键。{SPACE KEY}

enter image description here

第4步:现在开始编写代码

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 = '<p>This page was created with Python!</p>'

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

0

我不懂Python,但可以通过REST调用来完成这个任务:

echo '{"type":"page","ancestors":[{"type":"page","id":'$CONFLUENCE_PARENT_PAGE'}],"title":"'$PAGE_NAME'","space":{"key":"'$CONFLUENCE_SPACE'"},"body":{"storage":{"value":"'$CONTENT'","representation":"storage"}}}' > body.json

curl --globoff --insecure --silent -u ${CONFLUENCE_USER}:${CONFLUENCE_PASSWORD} -X POST -H 'Content-Type: application/json' --data @body.json $CONFLUENCE_REST_API_URL

Confluence的REST API URL看起来像这样: https://confluence.yourdomain.com/rest/api/content/

基本上,回答你的问题是,在创建全新页面时,你需要将父页面作为祖先一并发送。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接