簡體   English   中英

在特定空間中創建新頁面,並使用REST API用python中的Confluence中的現有文件內容對其進行更新?

[英]Create a New page in a specific space and update it with existing file Contents in Confluence in python using REST API?

我正在編寫一個Python腳本以在Confluence上創建一個新頁面並使用html文件中的內容更新該頁面。以下是我的代碼。我正在使用Confluence版本5.8.10

import argparse
import getpass
import sys

import json
import keyring
import requests

#-----------------------------------------------------------------------------
# Globals

BASE_URL = "https://wiki.hurry.com/rest/api/content"

def pprint(data):
    '''
    Pretty prints json data.
    '''
    print json.dumps(
        data,
        sort_keys = True,
        indent = 4,
        separators = (', ', ' : '))


def write_data(auth, html, title):

    ver = 'TEST'

    data = {
        'type' : 'page',
        'title' : str(title),
        'space' : {'key' : TEST},
        'body'  : {
            'storage' :
            {
                'representation' : 'storage',
                'value' : str(html),
            }
        }
    }

    data = json.dumps(data)

    print data

    url = '{base}/?os_authType=basic'.format(base = BASE_URL)
    print url
    r = requests.post(
        url,
        data = data,
        auth = auth,
        headers = { 'Content-Type' : 'application/json' }
    )

    r.raise_for_status()



def get_login(username = None):
    '''
    Get the password for username out of the keyring.
    '''

    if username is None:
        username = getpass.getuser()

    passwd = keyring.get_password('confluence_script', username)

    if passwd is None:
        passwd = getpass.getpass()
        keyring.set_password('confluence_script', username, passwd)

    return (username, passwd)


def main():

    parser = argparse.ArgumentParser()

    parser.add_argument(
        "-u",
        "--user",
        default = getpass.getuser(),
        help = "Specify the username to log into Confluence")

    parser.add_argument(
        "-t",
        "--title",
        default = None,
        type = str,
        help = "Specify a new title")

    parser.add_argument(
        "-f",
        "--file",
        default = None,
        type = str,
        help = "Write the contents of FILE to the confluence page")

    parser.add_argument(
        "html",
        type = str,
        default = None,
        nargs = '?',
        help = "Write the immediate html string to confluence page")

    options = parser.parse_args()

    auth = get_login(options.user)

    if options.html is not None and options.file is not None:
        raise RuntimeError(
            "Can't specify both a file and immediate html to write to page!")

    if options.html:
        html = options.html

    else:

        with open(options.file, 'r') as fd:
            html = fd.read()

    write_data(auth, html, options.title)


if __name__ == "__main__" : main()

以下是我得到的錯誤

  r.raise_for_status()
  File "C:\Python27\lib\site-packages\requests\models.py", line 928, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://wiki.hurry.com/rest/api/content/?os_authType=basic

有人可以讓我知道我在做什么錯嗎?

根據您收到的400 Client Error ,問題肯定就在您這邊。 RESTful API通常期望JSON數據,而不是HTML。

確保有關您正在發送的數據和使用請求的方式,因為請求會根據您傳遞給某些方法的內容在“幕后”執行其他操作(例如,將json參數添加到post會自動附加Content-Type: application/json您請求中的Content-Type: application/json標頭)。

另外,請確保您了解HTTP響應代碼 ;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM