3

我尝试使用 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 页面上给出的 stock 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))
4

2 回答 2

2

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

然后,请决定您要使用哪种 API 方法。要创建新内容,您必须向 发送POST请求rest/api/content,但要更新现有内容,您必须向 发送PUT请求rest/api/content/{contentId}

我刚刚注意到,您正在使用-ucurl 中的选项和auth请求库的方法进行身份验证。我不太确定,但我认为不可能通过这些技术进行身份验证。Atlassian 在他们的文档中列出了可能性,我认为您必须自己实现他们的基本身份验证

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

Atlassian 提供了一个REST API 浏览器插件来测试 API 请求。如果你不能在你的 Confluence 实例中安装插件,你可以使用浏览器扩展(例如YARC)。这样,您可以向 Confluence 发送请求,而无需关心身份验证。

于 2017-02-16T16:34:54.133 回答
2

我接受了 lukegv 的回答,因为它最终给了我一些错误消息,我可以使用这些错误消息来获得最终解决方案。更重要的是,我发布我的脚本以防万一有人遇到这种情况并且对缺少 confluence API 文档感到愤怒。

@lukegv 是对的——尽管更新 confluence 的文档说要使用这个:

http://localhost:8080/confluence/rest/api/content/3604482

如果您使用的是 wiki 站点,您应该使用这个

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'}))
于 2017-02-16T21:40:36.447 回答