業務でコンフルを使っているのですが、ページの作成をどうにか自動で出来ないかと思い、APIを探してみたらあったのでさっそく使ってみました。
以下にAPIのサンプルがあるので、curlですがすぐに試すことができます。
Confluence REST API Examples
リファレンスはこちら
curlで実行してみる
まずはサンプルにある通りcurlで作ってみる。
curl -u username:password -X POST -H 'Content-Type: application/json' -d'{"type":"page","title":"new page","space":{"key":"TST"},"body":{"storage":{"value":"<p>This is a new page</p>","representation":"storage"}}}' http://your_confluence_domain/rest/api/content/
どこかの子ページとして作りたい場合はこっち
curl -u username:password -X POST -H 'Content-Type: application/json' -d'{"type":"page","title":"new page", "ancestors":[{"id":1234}], "space":{"key":"TST"},"body":{"storage":{"value":"<p>This is a new page</p>","representation":"storage"}}}' http://your_confluence_domain/rest/api/content/
どうやら"ancestors":[{"id":1234}]
で親ページを指定している模様。
Pythonで新規ページを作る
指定するパラメータはcurlのときと同様。
※一部抜粋
import requests
def main():
payload = {
'type': 'page',
'title': 'new page',
'space': {
'key': 'TST'
},
'ancestors': [{'id': 1234}],
'body': {
'storage': {
'value': '<p>This is a new page</p>,
'representation': "storage"
}
}
}
headers = {
'Content-Type': 'application/json',
'Authorization': 'Basic ' + base64.b64encode('username:password')
}
url = 'http://your_confluence_domain/rest/api/content/'
response = requests.post(url, data = json.dumps(reqdata), headers = headers)
response.raise_for_status()