Mad*_*uan 6 python confluence confluence-rest-api
我正在尝试使用 Python 的xmlrpclib. 我已经知道如何更新现有页面的内容,但如何创建一个全新的页面?
我使用以下脚本来更新内容:
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)
Run Code Online (Sandbox Code Playgroud)
这在更新内容时效果很好。但问题是,pageID在创建新页面时我需要以某种方式解决,但我不知道该怎么做。
有没有其他方法可以创建新页面?
小智 8
您可以使用 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))
Run Code Online (Sandbox Code Playgroud)
“J. Antunes”的答案是正确的,但我花了很长时间才找到 API、pageIds 等。这个答案将为您提供有关如何使用API Tokens实现这一目标的分步指南。
第一步:在 Confluence 中生成 API Token。
在 Confluence 页面中,转到“设置”并单击“密码”。单击“创建和管理 API 令牌”并获取令牌。{令牌}
第 2 步:找到您要创建子页面的父页面
去获取父页面ID,找到{Parent Page ID}
第 3 步:获取空格键
在汇合处,转到 Space Settings,然后找到提到的 Space Key。{空格键}
第 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))
Run Code Online (Sandbox Code Playgroud)
我不懂 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
Run Code Online (Sandbox Code Playgroud)
Confluence Rest api url 看起来像这样: https ://confluence.yourdomain.com/rest/api/content/
基本上,您问题的答案是您需要在创建全新页面时将父页面作为祖先发送。
| 归档时间: |
|
| 查看次数: |
10781 次 |
| 最近记录: |