“J. Antunes”的答案是正确的,但我花了很长时间才找到 API、pageIds 等。这个答案将为您提供有关如何使用API Tokens实现此目的的分步指南。
第 1 步:在 Confluence 中生成 API 令牌。
在汇合页面中,转到设置并单击密码。单击“创建和管理 API 令牌”并获取令牌。{代币}
第 2 步:找到要在其中创建子页面的父页面
去获取父页面ID,找到{Parent Page ID}
第 3 步:获取空格键
在汇合处,转到空间设置,并找到提到的空间键。{空格键}
第 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))