【发布时间】:2017-05-10 09:46:44
【问题描述】:
我想通过Confluence REST API更新一个confluence页面。请建议一个代码 sn-p,我可以使用它通过其“页面标题”更新页面。
假设我的confluence站点是https://wiki.mydomain.com,页面标题是TEST,空格是TST。
【问题讨论】:
标签: python python-2.7 confluence-rest-api
我想通过Confluence REST API更新一个confluence页面。请建议一个代码 sn-p,我可以使用它通过其“页面标题”更新页面。
假设我的confluence站点是https://wiki.mydomain.com,页面标题是TEST,空格是TST。
【问题讨论】:
标签: python python-2.7 confluence-rest-api
您还可以使用Atlassian Python API 的Confluence module 从 Python 更新/删除 Confluence 页面。这基本上是 REST API 的包装器,用于提供简单的接口。
通过pip安装:
pip install atlassian-python-api
用法:
from atlassian import Confluence
conf_site = 'https://wiki.mydomain.com'
conf_user = 'username'
conf_pass = 'password'
page_title = 'TEST'
page_space = 'TST'
# connect to Confluence
conf = Confluence(url=conf_site, username=conf_user, password=conf_pass)
# resolve page ID
page_id = conf.get_page_id(page_space, page_title)
# optonal: get current page content, in case you want to base your editing on that
page = conf.get_page_by_id(page_id, expand='body.storage')
page_content = page['body']['storage']['value']
# new page content
page_content = '<p>This is the new updated text of the page</p>'
# update page with new content
conf.update_page(page_id, page_title, page_content)
或者,删除:
# This method removes a page, if it has recursive flag, method removes including child pages
conf.remove_page(page_id, status=None, recursive=False)
【讨论】:
正如您在 Atlassian 文档 (here) 中看到的,您可以通过关注 curl 更新页面:
curl -u admin:admin -X PUT -H 'Content-Type: application/json' -d'{"id":"3604482","type":"page",
"title":"new page","space":{"key":"TST"},"body":{"storage":{"value":
"<p>This is the updated text for the new page</p>","representation":"storage"}},
"version":{"number":2}}' http://localhost:8080/confluence/rest/api/content/3604482 | python -mjson.tool
但它适用于页面 ID 而不是页面标题。您可以通过以下方式获取 id:
curl -u admin:admin -X GET "http://localhost:8080/confluence/rest/api/content?title=myPage%20Title
&spaceKey=TST&expand=history" | python -mjson.tool
顺便说一句,由于您看起来像一个新用户,因此我们不会在这里提供代码 sn-p,您需要告诉我们您尝试了什么以及您的实际问题是什么。我建议你也看看How do I ask a good question :-)
【讨论】:
以下是使用页面标题删除 Confluence 页面的 Python 代码。
def deletePageByTitle(title):
checkPageExistsData = requests.get("https:\wiki.mydomain.com/rest/api/content?title=" + title + "&expand=history", headers={'Content-Type':'application/json'}, auth=('yourConfluenceUser', 'yourConfluecePassword'))
requestJson = checkPageExistsData.json()
pageId = ''
if requestJson["results"] != None:
for results in requestJson["results"]:
pageId = (results["id"])
requests.delete("https:\wiki.mydomain.com/rest/api/content/"+pageId+"", headers={'Content-Type':'application/json'}, auth=('yourConfluenceUser', 'yourConfluecePassword'))
print('Page deleted')
else:
print('Page does not exist')
对于您的情况,此函数将被称为:deletePageByTitle("TEST")。 希望这会有所帮助!
【讨论】:
使用 Python 3 更新 Confluence 上的现有 Wiki 页面。请参阅 this answer to know how to create a page 使用 Python。
确保您尝试更新的页面的 ID 正确无误。如果您要更改标题,请确保相同的标题不会出现在同一空间的另一个页面上。
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 Existing Page'
page_html = '<p>This is the text that needs to be updated</p>'
page_id = {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/{Page ID}'
# Request Headers
headers = {
'Content-Type': 'application/json;charset=iso-8859-1',
}
# Request body
data = {
'id': {Page ID}
'type': 'page',
'title': page_title,
'space': {'key':space_key},
'body': {
'storage':{
'value': page_html,
'representation':'storage',
}
},
'version': {
'number': 2,
'when': '2017-10-06T15:16:17.501-04:00'}
}
# the version number should be the immediate next version number of the existing document.
# We're ready to call the api
try:
r = requests.put(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 Updated!')
except requests.exceptions.RequestException as e:
# A serious problem happened, like an SSLError or InvalidURL
print("Error: {}".format(e))
【讨论】: