在特定 space 中创建一个新页面并使用 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 中的内容更新该页面 file.Below 是我的 code.I使用合流版本 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。

请确定您发送的数据以及您使用 Request 的方式,因为 Request 会根据您传递给某些方法的内容执行额外的操作 "behind the scenes"(例如添加 json post 的参数将自动在您的请求中附加 Content-Type: application/json header。

此外,请务必理解 HTTP response codes ;)