Confluence Rest API中的存储格式为主体(页面创建)

时间:2016-01-14 21:10:51

标签: php confluence confluence-rest-api

我正在使用PHP开发页面导入程序,我遇到了使用存储格式创建页面的问题。基本上我收到了格式错误的xhtml错误:

HTTP/1.1 400 Bad Request
Date: Thu, 14 Jan 2016 18:13:48 GMT
Server: Apache-Coyote/1.1
X-ASEN: SEN-6458441
X-Seraph-LoginReason: OK
X-AUSERNAME: XXX
Cache-Control: no-cache, must-revalidate
Expires: Thu, 01 Jan 1970 00:00:00 GMT
X-Content-Type-Options: nosniff
Content-Type: application/json
Connection: close
Transfer-Encoding: chunked

{
  "statusCode": 400,
  "data": {
    "authorized": false,
    "valid": true,
    "errors": []
  },
  "message": "Error parsing xhtml"
}

goad是创建phpDocumentor之类的源文档,因此我手动创建了页面,其中包含了正确显示类文档所需的所有宏。问题是当我使用"查看存储格式"选项,我得到的XML无效:

<ac:layout><ac:layout-section ac:type="single"><ac:layout-cell>
<p>Test</p><ac:structured-macro ac:macro-id="3e74101c-857f-4c47-8618-a27c627eea2c" ac:name="info" ac:schema-version="1"><ac:parameter ac:name="title">Fully qualified class name</ac:parameter><ac:rich-text-body>\Zend\Authentication\Adapter\Digest</ac:rich-text-body></ac:structured-macro>
<p><a href="https://github.com/zendframework/zend-authentication/blob/master/src/Adapter/Digest.php">Browse source code</a></p></ac:layout-cell></ac:layout-section><ac:layout-section ac:type="two_left_sidebar"><ac:layout-cell>
<h2>Class contents</h2>
<p><ac:structured-macro ac:macro-id="726aa6fa-6e48-4005-95ad-0fb28aae8162" ac:name="toc" ac:schema-version="1"><ac:parameter ac:name="maxLevel">4</ac:parameter><ac:parameter ac:name="minLevel">3</ac:parameter></ac:structured-macro></p>
<p>&nbsp;</p></ac:layout-cell><ac:layout-cell>
<h2>Description</h2>
<p><ac:structured-macro ac:macro-id="c7feab99-a70b-4ab8-bbac-846ed981bed0" ac:name="excerpt" ac:schema-version="1"><ac:parameter ac:name="atlassian-macro-output-type">INLINE</ac:parameter><ac:rich-text-body>
<p>Digest adapter for Zend Authentication mechanism</p></ac:rich-text-body></ac:structured-macro>&nbsp;</p>
<h2>Class details</h2>
<table style="letter-spacing: 0.1px;">
<tbody>
<tr>
<th>Package</th>
<td>\Zend\Authentication\Adapter</td></tr>
<tr>
<th>Inherited from</th>
<td><span style="color: rgb(68,68,68);">\Zend\Authentication\Adapter\AbstractAdapter</span></td></tr></tbody></table>
<h3>Methods</h3>
<hr /><ac:structured-macro ac:macro-id="43484e81-d0fd-4d40-b990-a7d90cd2b565" ac:name="panel" ac:schema-version="1"><ac:parameter ac:name="borderStyle">solid</ac:parameter><ac:rich-text-body>
<h4>&nbsp;<span style="color: rgb(51,51,51);">::__construct()</span></h4>
<hr />
<p><span style="color: rgb(51,51,51);">&nbsp;</span></p>
<p>Sets adapter options</p><ac:structured-macro ac:macro-id="323db0b7-4961-4b55-8332-43790ef9b2bc" ac:name="code" ac:schema-version="1"><ac:parameter ac:name="language">php</ac:parameter><ac:parameter ac:name="theme">Confluence</ac:parameter><ac:plain-text-body><![CDATA[__construct(mixed $filename = null, mixed $realm = null, mixed $identity = null, mixed $credential = null)]]></ac:plain-text-body></ac:structured-macro>
<h5>Parameters</h5><ac:structured-macro ac:macro-id="eda8fe1a-8ccd-4b7b-8942-99fd9c2dba18" ac:name="expand" ac:schema-version="1"><ac:rich-text-body>
<table>
<tbody>
<tr>
<th>Parameter</th>
<th>Data type</th>
<th>Description</th></tr>
<tr>
<td>$filename</td>
<td>mixed</td>
<td>&nbsp;</td></tr>
<tr>
<td>$realm</td>
<td>mixed</td>
<td>&nbsp;</td></tr></tbody></table></ac:rich-text-body></ac:structured-macro></ac:rich-text-body></ac:structured-macro></ac:layout-cell></ac:layout-section></ac:layout>

因此,当我在REST API调用中使用它时,我之前收到了错误。电话看起来像这样:

<?php

use GuzzleHttp\Client;
use GuzzleHttp\Post\PostBody;
use GuzzleHttp\Stream\StreamInterface;
use GuzzleHttp\Exception\RequestException;

function send_request() {
    // My API (2) (POST http://confluence.dev/rest/api/content)

    $client = new Client();
    $request = $client->createRequest('POST', 'http://confluence.dev/rest/api/content');
    $postBody = $request->getBody();

    try {
        $request->addHeaders([
            'Authorization'=>'Basic XXX',
            'Content-Type'=>'application/json',
        ]);
        $body = [
            "body" => [
                "storage" => [
                    "value" => "<ac:layout-section ac:type=\"single\"><ac:layout-cell><ac:structured-macro ac:macro-id=\"3e74101c-857f-4c47-8618-a27c627eea2c\" ac:name=\"info\" ac:schema-version=\"1\"><ac:parameter ac:name=\"title\">Fully qualified class name</ac:parameter><ac:rich-text-body>\\Zend\\Authentication\\Adapter\\Digest</ac:rich-text-body></ac:structured-macro>
<p><a href=\"https://github.com/zendframework/zend-authentication/blob/master/src/Adapter/Digest.php\">Browse source code</a></p></ac:layout-cell></ac:layout-section><ac:layout-section ac:type=\"two_left_sidebar\"><ac:layout-cell>
<h2>Class contents</h2>
<p><ac:structured-macro ac:macro-id=\"726aa6fa-6e48-4005-95ad-0fb28aae8162\" ac:name=\"toc\" ac:schema-version=\"1\"><ac:parameter ac:name=\"maxLevel\">4</ac:parameter><ac:parameter ac:name=\"minLevel\">3</ac:parameter></ac:structured-macro></p>
<p>&nbsp;</p></ac:layout-cell><ac:layout-cell>
<h2>Description</h2>
<p><ac:structured-macro ac:macro-id=\"c7feab99-a70b-4ab8-bbac-846ed981bed0\" ac:name=\"excerpt\" ac:schema-version=\"1\"><ac:parameter ac:name=\"atlassian-macro-output-type\">INLINE</ac:parameter><ac:rich-text-body>
<p>Digest adapter for Zend Authentication mechanism</p></ac:rich-text-body></ac:structured-macro>&nbsp;</p>
<h2>Class details</h2>
<table style=\"letter-spacing: 0.1px;\">
<tbody>
<tr>&& count($stdBody) == 0
<th>Package</th>
<td>\\Zend\\Authentication\\Adapter</td></tr>
<tr>
<th>Inherited from</th>
<td><span style=\"color: rgb(68,68,68);\">\\Zend\\Authentication\\Adapter\\AbstractAdapter</span></td></tr></tbody></table>
<h3>Methods</h3>
<hr /><ac:structured-macro ac:macro-id=\"43484e81-d0fd-4d40-b990-a7d90cd2b565\" ac:name=\"panel\" ac:schema-version=\"1\"><ac:parameter ac:name=\"borderStyle\">solid</ac:parameter><ac:rich-text-body>
<h4>&nbsp;<span style=\"color: rgb(51,51,51);\">::__construct()</span></h4>
<hr />
<p><span style=\"color: rgb(51,51,51);\">&nbsp;</span></p>
<p>Sets adapter options</p><ac:structured-macro ac:macro-id=\"9de243a9-ca88-45e1-a6d4-7ff9b542babd\" ac:name=\"code\" ac:schema-version=\"1\"><ac:parameter ac:name=\"language\">php</ac:parameter><ac:parameter ac:name=\"theme\">Confluence</ac:parameter><ac:plain-text-body><![CDATA[__construct(mixed $filename = null, mixed $realm = null, mixed $identity = null, mixed $credential = null)]]></ac:plain-text-body></ac:structured-macro>
<h5>Parameters</h5><ac:structured-macro ac:macro-id=\"a2230db4-ea43-4e99-89d9-2661a57fd5c3\" ac:name=\"expand\" ac:schema-version=\"1\"><ac:rich-text-body>
<table>
<tbody>
<tr>
<th>Parameter</th>
<th>Data type</th>
<th>Description</th></tr>
<tr>
<td>$filename</td>
<td>mixed</td>
<td>&nbsp;</td></tr>
<tr>
<td>$realm</td>
<td>mixed</td>
<td>&nbsp;</td></tr></tbody></table></ac:rich-text-body></ac:structured-macro></ac:rich-text-body></ac:structured-macro></ac:layout-cell></ac:layout-section>",
                    "representation" => "storage"
                ]
            ],
            "title" => "Adam tests",
            "space" => [
                "key" => "TEST"
            ],
            "type" => "page"
        ];
        $request->setBody(GuzzleHttp\Stream\Stream::factory(json_encode($body)));

        $response = $client->send($request);
        echo "Response HTTP : " . $response->getStatusCode();
    }
    catch (RequestException $e) {
        echo "HTTP Request failed\n";
        echo $e->getRequest();
        if ($e->hasResponse()) {
            echo $e->getResponse();
        }
    }
}

我使用Confluence 5.9.4。

我试图创建此页面的方式有问题吗?页面中的存储格式是否与API不兼容?

2 个答案:

答案 0 :(得分:1)

您可以尝试为存储格式创建这样的网址:

http://localhost:8099/rest/api/content/<pageID>?expand=body.storage,version

答案 1 :(得分:0)

据我所知,即使截至2017年5月,您也无法将XML格式发布到Confluence。以下是我尝试在Powershell中发布XML字符串但是收到错误请求错误。尝试发布像</ac:layout-cell><ac:layout-cell>这样的空标记会引发错误的请求错误,因此我假设Confluence正在尝试解析存储值,我们需要一种方法来防止这种情况并且只发布原始值。

add-type @"
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    public class TrustAllCertsPolicy : ICertificatePolicy {
        public bool CheckValidationResult(
            ServicePoint srvPoint, X509Certificate certificate,
            WebRequest request, int certificateProblem) {
            return true;
        }
    }
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

$uri = "https://confluence.dev/rest/api/content/"
$headers = @{}
$headers.Add("Authorization", "Basic XXX")
$headers.Add("Content-Type", "application/json")

$temp = get-content .\temp.xml -raw
$body =  '{"type":"page","title":"Auto Gen Test","space":{"key":"SAN"},"body":{"storage":{"value":"'+($temp)+'","representation":"storage"}}}'

$result = Invoke-RestMethod -Method POST -Headers $headers -Uri $uri -Body $body