6

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)

これは、コンテンツを更新するときにうまく機能します。しかし、問題は、新しいページを作成するときに何らかの方法で解決する必要があり、pageIDその方法がわかりません。

新しいページを作成する他の方法はありますか?

4

3 に答える 3

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))
于 2017-11-24T01:05:38.347 に答える
6

「J. Antunes」の回答は正しいですが、 APIや pageId などを見つけるのに時間がかかりました。

ステップ 1: Confluence で API トークンを生成します。

合流ページで、[設定] に移動し、[パスワード] をクリックします。「API トークンの作成と管理」をクリックして、トークンを取得します。{トークン}

ここに画像の説明を入力

ステップ 2: 子ページを作成する親ページを見つける

親ページ ID の取得に移動し、 {親ページ 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))
于 2019-12-20T06:09:18.963 に答える
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/

基本的に、あなたの質問に対する答えは、新しいページを作成するときに親ページを祖先として送信する必要があるということです。

于 2015-11-09T14:35:31.310 に答える