2015-10-16 28 views
2

我正在嘗試使用Python的xmlrpclib創建一個融合的新頁面。我已經知道如何更新現有頁面的內容,但是如何創建一個全新的頁面?我如何創建一個新的頁面來與Python融合

我用下面的腳本來更新內容:

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,並且我不知道如何做到這一點。

有沒有其他方法可以創建新頁面?

回答

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 

匯合REST API的URL看起來是這樣的: https://confluence.yourdomain.com/rest/api/content/

基本上回答你的問題是你需要在創建全新頁面時將父頁作爲祖先發送。

0

您可以使用該合流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)) 
相關問題