2

我正在使用 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"
}

目的是创建 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 不兼容?

4

2 回答 2

1

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

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

于 2016-07-20T19:14:35.327 回答
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
于 2017-04-26T14:07:05.307 回答