如何创建新页面以与Python融合 [英] How can I create a new page to confluence with Python

查看:111
本文介绍了如何创建新页面以与Python融合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python的 xmlrpclib 创建一个融合的新页面。我已经知道如何更新现有页面的内容,但是如何创建一个全新的页面?

I am trying to create a new page into confluence using Python's xmlrpclib. I already know how to update content of an existing page, but how can I create a totally new page?

我使用以下脚本来更新内容:

I have used following script to update content:

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 ,我不知道该怎么做。

and that works well when updating content. But problem is that somehow I need to resolve pageID when creating a new page and I have no idea how to do that.

还有其他创建新页面的方法吗?

Are there any other ways to create new page?

推荐答案

您可以使用Confluence REST API创建页面:
https:// docs.atlassian.com/atlassian-confluence/REST/latest-server/

You can create pages using the Confluence REST API: https://docs.atlassian.com/atlassian-confluence/REST/latest-server/

以下是在Python3中有效的示例。您需要知道父页面ID。

Here is an example that works in Python3. You'll need to know the parent page 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))

这篇关于如何创建新页面以与Python融合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆