python-Confluence WIki页面未通过API更新

我试图使用Rest API更新我们的Wiki页面,但是即使我收到200条代码,似乎也没有任何反应.

我已经尝试通过邮递员和Python来完成此任务,并且在两种情况下我都收到相同的服务器响应,但无济于事.
这是我的Python代码-

curl = 'curl -u user:pass -X POST -H \'Content-Type: application/json\' ' \
           '-d \'{0}\' https://wiki.myCompany.com:8444/confluence/rest/api/content/'\
            .format(json.dumps(new))
    output = subprocess.check_output(['bash', '-c', curl])
    print(output`)

我试过同时使用POST和PUT

这是回应-

PUT https://wiki.myCompany.com:8444/confluence/rest/api/content/
 200 OK       26.47 kB       655 ms
View Request View Response
HEADERS
Content-Encoding: gzip
Content-Length: 6578
Content-Security-Policy: frame-ancestors 'self'
Content-Type: text/html;charset=UTF-8
Date: Wed, 15 Feb 2017 20:24:46 GMT
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=DBCAA4C03DC489A720B8A59D755BD22A; Path=/; Secure; HttpOnly
Vary: User-Agent
X-Accel-Buffering: no
X-Asen: SEN-3386858
X-Ausername: username
X-Confluence-Request-Time: 1487190286413
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Seraph-Loginreason: OK
X-Xss-Protection: 1; mode=block

BODYview raw


<!DOCTYPE html>
<html>
  <head>
    <title>Dashboard - myCompany Wiki</title>
    <meta http-equiv="X-UA-Compatible" content="IE=EDGE,chrome=IE7">
      <meta charset="UTF-8">
        <meta id="confluence-context-path" name="confluence-context-path" content="">
          <meta id="confluence-base-url" name="confluence-base-url" content="https://wiki.myCompany.com:8444">
            <meta id="atlassian-token" name="atlassian-token" content="abcd227f923fa6d5cce068a25de3bb4a3a3ceca4">
              <script type="text/javascript">
        var contextPath = '';
</script>

..... A lot more html .... but nothing relating to Body or Body.Storage...

我的JSON格式正确,并且包含页面ID-这是它的开头.

{"id":"28870287","type":"page","status":"current","title":"Automated QA Results - Android","body":{"storage":{"value":"<p>These are the results of every git merge...}}

有谁知道为什么什么也没发生?

我之前已经通过请求库尝试过此操作-得到了相同的200个响应代码.我只是看你们是否注意到呼叫本身有问题而不是实施

   # output = requests.post('https://{0}/confluence/rest/api/content'.format(jirasite),
#                     data=(json.dumps(new)),
#                     auth=('user', 'pass'),
#                     headers=({'Content-Type': 'application/json'}))

****新更新****

我正在从Confluence API页面上给出的字面量卷曲值中尝试

'{"id":"28870287","type":"page","title":"new page","space":{"key":"TST"},"body":{"storage":{"value":"<p>This is the updated text for the new page</p>","representation":"storage"}},"version":{"number":2}}'

仍然无济于事…我完全不知所措…

******再次更新******

我将要发布当前正在使用的代码

    r = requests.get('{0}/rest/api/content/28870287?expand=body.storage,version'.format(conflu_site),
                 auth=(test_user, test_pass)).text

    print(r) # This works as expected

new = '{\"id\":\"28870287\",\"type\":\"page",\"title":\"Automated QA Results - Android\",\"space\":{\"key\":\"TST\"},' \
      '\"body\":{\"storage\":{\"value\":\"<p>This is the updated text for the new page</p>\",' \
      '\"representation\":\"storage\"}},\"version\":{\"number\":2}}'

update_response = requests.put('{0}/confluence/rest/api/content/28870287/'.format(conflu_site),
                                data=new,
                                auth=(test_user, test_pass),
                                headers=({'Content-Type': 'application/json'}))

print("Update Confluence Response: " + str(update_response))

解决方法:

首先,请检查Confluence实例的基本URL,因为默认情况下它看起来像xxx.xxx.xxx.xxx:1990/confluence,但是可以摆脱上下文融合.在您的示例中,您将wiki.myCompany.com:8444用作jirasite,并且仍在REST API URL rest / api之前添加了合流.如果使用正确的API端点,则永远不会收到HTML响应.

然后,请决定要使用哪种API方法.要获得create的新内容,您必须向rest / api / content发送POST请求,而要获得update现有的内容,则必须向rest / api / content / {contentId}发送一个PUT请求.

我刚刚注意到,您正在curl中使用-u选项,并使用请求库的auth方法进行身份验证.我不太确定,但我认为无法通过这些技术进行身份验证. Atlassian在其文档中列出了the possibilities,我认为您必须自己实现其Basic身份验证.

其他一些疑难解答提示:

Atlassian提供了REST API Browser Plugin来测试API请求.如果您无法在Confluence实例中安装插件,则可以使用浏览器扩展程序(例如YARC).这样,您可以将请求发送到Confluence,而无需考虑身份验证.

上一篇:java-Tomcat在路径上的Apache代理错误后


下一篇:如何使用REST API从Python脚本更新Jira v5.1中的问题