Confluence Wiki页面通过API未更新

3

我尝试使用Rest API更新我们的维基页面,但即使我收到了200代码,似乎什么也没有发生。

我已经尝试通过Postman和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...}}

有人知道为什么什么都没有发生吗?

我之前尝试过使用requests库 - 我得到了相同的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页面上给出的默认curl值。

'{"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))

1
首先,你现在的做法有点混乱。不要使用Python来调用curl二进制文件,你最好干脆不用Python。如果可以的话,请使用“requests”模块,这样你就可以清理代码并保持全部纯Python。然后再试一次,让我们知道你得到了什么响应。网络上关于使用“requests”的资源可能比其他任何Python库都多,所以你不应该有找不到需要的东西的麻烦。 - daveruinseverything
edited/updated. - Nefariis
2个回答

3
首先,请检查您的Confluence实例的基本URL,因为默认情况下它看起来像是xxx.xxx.xxx.xxx:1990/confluence,但可以去掉上下文confluence。在您的示例中,您使用wiki.myCompany.com:8444作为称为jirasite的东西,并在REST API url rest/api之前仍然添加confluence。如果使用正确的API端点,则应该永远不会收到HTML响应。
然后,请决定要使用哪个API方法。要创建新内容,您必须向rest/api/content发送POST请求,但要更新现有内容,您必须向rest/api/content/{contentId}发送PUT请求。
我刚注意到,您在curl中使用了-u选项和请求库的auth方法进行身份验证。我不太确定,但我认为这些技术无法进行身份验证。Atlassian在其文档中列出了可能性,我认为您必须自己实现他们的基本身份验证

一些额外的故障排除提示:

Atlassian提供REST API浏览器插件来测试API请求。如果您无法在Confluence实例中安装插件,则可以使用浏览器扩展程序(例如YARC)。这样,您就可以发送请求到Confluence而不必担心身份验证。


1
我接受了这个作为答案,因为我非常感激帮助 - 我永远不会想到 Confluence 不应该在请求调用中。但无论如何,我发布了最终的解决方案,以防有人遇到此问题需要帮助。 - Nefariis

3
我接受了lukegv的答案,因为它最终给了我一些错误信息,我可以借此得到最终的解决方案。更重要的是,我发布我的脚本,以防有人遇到这个问题并对confluence API缺乏文档感到恼火。
@lukegv是正确的 - 即使更新confluence的文档说要使用这个:
http://localhost:8080/confluence/rest/api/content/3604482

如果您正在使用维基站点,则应该使用此选项。
http://localhost:8080/rest/api/content/3604482

第二个问题是,您需要增加页面版本号才能更新。要做到这一点,您需要像这样调用expand=version。
get_json = json.loads(requests.get('{0}/rest/api/content/28870287?expand=body.storage,version'.format(conflu_site),
                 auth=(test_user, test_pass)).text)

以下是我最终实现代码的过程。

# This is the bare minimum that it takes to update a wiki page
update_templete = json.loads('{"id":"28870287","type":"page","body":{"storage":' \
              '{"value":"Test","representation":"storage"}},"version":{"number":2}}')

# This is used to get the current body of the wiki page 
get_json = json.loads(requests.get('{0}/rest/api/content/28870287?expand=body.storage,version'.format(conflu_site),
                 auth=(test_user, test_pass)).text)

# This increments the version number by one
update_templete["version"]["number"] = get_json["version"]["number"] + 1

# This saves the current page's body as a string
body = str(get_json["body"]["storage"]["value"])

# Use this to change the body however you see fit 
new_body = do_Stuff(body) 

# Update the templete's body json with the new body
update_templete["body"]["storage"]["value"] = new_body

# updates the confluence wiki site with the page body
update_response = requests.put('{0}/rest/api/content/28870287/'.format(conflu_site),
                                data=json.dumps(update_templete),
                                auth=(test_user, test_pass),
                                headers=({'Content-Type': 'application/json'}))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接