【问题标题】:How to upload Child page to Confluence using REST Api/JSON object with python?如何使用带有 python 的 REST Api/JSON 对象将子页面上传到 Confluence?
【发布时间】:2016-05-31 08:48:45
【问题描述】:

我想创建一个程序,定期将我们的 git repo 中的更改上传到 confluence。 到目前为止,我有这些: - 程序从 git 存储库中收集更改 - 根据预定义的 html 模板对其进行格式化并将其保存到文本文件中 - 后来用python解析成JSON对象

问题是我以前没有使用过rest api,我不知道如何使用它将更改作为子页面上传到汇合中的现有页面。 我搜索了很多,但我没有找到任何适合我需要的解决方案,或者由于缺乏这方面的知识而无法理解它们。

【问题讨论】:

    标签: python json git rest confluence


    【解决方案1】:

    Confluences REST API 文档可用here

    根据您需要更新的文档,需要以下 uri 来执行您的请求:

    /rest/api/content/{contentId}
    

    如果您从未使用 python 完成过 API 请求,那么根据您使用的 python 版本,可以使用多个库。 requests, Http.client, urllib 1,2,3 等等..

    要执行一个简单的经过身份验证的请求,您很可能需要一个由 confluence 或管理员凭据提供的令牌:

    可以在developers.atlassian.com 找到请求示例 向页面添加评论:

    import requests, json
    
    def printResponse(r):
        print '{} {}\n'.format(json.dumps(r.json(), sort_keys=True, indent=4, separators=(',', ': ')), r)
    
    r = requests.get('http://localhost:8080/confluence/rest/api/content',
        params={'title' : 'Page title to comment on'},
        auth=('admin', 'admin'))
    
    printResponse(r)
    
    parentPage = r.json()['results'][0]
    
    pageData = {'type':'comment', 'container':parentPage,
        'body':{'storage':{'value':"<p>A new comment</p>",'representation':'storage'}}}
    r = requests.post('http://localhost:8080/confluence/rest/api/content',
        data=json.dumps(pageData),
        auth=('admin','admin'),
        headers=({'Content-Type':'application/json'}))
    
    printResponse(r)
    

    【讨论】:

    • 问题是,我想将子页面上传到现有页面,而不是更新其内容。我也在使用python 2.7。如果我在一个步骤中创建子页面并在另一个步骤中更新它,那么您编写的这个解决方案对我有好处。但我想一步到位。此外,developers.atlassian.com 链接已损坏。
    • 我已经更新了链接,所以。您需要上传附件吗?
    • 不,我必须创建一个新页面,但要在现有页面下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-22
    相关资源
    最近更新 更多