OneNote API "A syntax or other error was detected in a request your app sent"

OneNote API "A syntax or other error was detected in a request your app sent"

我正在 php 上编写一个库以与 OneNote API 通信(基于 https://github.com/OneNoteDev/OneNoteAPISamplePHP)。

我想创建版块和页面。

该部分创建的很好,但是当我尝试创建页面时总是出现错误:

A syntax or other error was detected in a request your app sent
error :{
       "code":"20011",
       "message":"The multi-part payload was malformed."
}

页面正文(混合文字+图片):

--4ba4cdfe92db7bf32a8958dcad26d539951229d6570cfa0626c0adff64bf1ed9
Content-Disposition:form-data; name="Presentation"
Content-Type:text/html

<!DOCTYPE html>
<html>
  <head>
  <title>Page 1</title>
  <meta name="created" content="2015-06-02T04:41:44-07:00" />
  </head>
  <body><img src="name:imageData1" alt="" width="488" height="181" /><p><p>Suspendisse enim turpis, dictum sed, iaculis a, condimentum nec, nisi. Praesent egestas neque eu enim. Suspendisse eu ligula. Vestibulum rutrum, mi nec elementum vehicula, eros quam gravida nisl, id fringilla neque ante vel mi. Morbi ac felis.</p>
</p></body>
</html>

--4ba4cdfe92db7bf32a8958dcad26d539951229d6570cfa0626c0adff64bf1ed9
Content-Disposition:form-data; name="imageData1"
Content-Type:application/octet-stream

SomeData

--4ba4cdfe92db7bf32a8958dcad26d539951229d6570cfa0626c0adff64bf1ed9--

页面正文(文本):

--5e4b8756b81ac1881459fe7691fb6a3e93e35540ccf71450095f6d5a679e27a5
Content-Disposition:form-data; name="Presentation"
Content-Type:text/html

<!DOCTYPE html>
<html>
  <head>
    <title>Page 1</title>
    <meta name="created" content="2015-06-02T04:45:33-07:00" />
  </head>
  <body><p><p>Nulla facilisi. Etiam vitae tortor. Curabitur suscipit suscipit tellus. Etiam rhoncus. Quisque ut nisi.</p>

<p>Suspendisse faucibus, nunc et pellentesque egestas, lacus ante convallis tellus, vitae iaculis lacus elit id tortor. Vestibulum facilisis, purus nec pulvinar iaculis, ligula mi congue nunc, vitae euismod ligula urna in dolor. Cras varius. Morbi mollis tellus ac sapien. Maecenas vestibulum mollis diam.</p>
</p></body>
</html>

--5e4b8756b81ac1881459fe7691fb6a3e93e35540ccf71450095f6d5a679e27a5--

我用这个 url 创建页面 https://www.onenote.com/api/v1.0/sections/$section_id/pages

我在测试服务器上上传并且工作正常。但是当我在实时服务器上上传时(哪个服务器安装了 ssl 认证)总是出现这个错误。

任何想法, 谢谢。

下面是一个非常简单的例子,说明如何做到这一点。 该示例显示了如何将两张图片嵌入到新页面的内容中 + 一张额外的 link 以供下载和嵌入图片。 您所要做的就是将您的访问令牌粘贴到对 $oneNoteMultiPartRequest->send() 的调用中,然后您就设置好了。

对于这个例子,我使用了一个嵌入代码中的图像,但你可以使用任何你想要的文件/图像(只记得更新 <object>Content-Type header 用于相应的请求部分。

请注意 HTML(<img src="name:..."><object data="name:...">)中的各种属性如何通过名称引用现有请求部分。

class OneNoteMultiPartRequest {
    protected $boundary;
    protected $parts = array();
    protected $responseInfo = null;
    protected $errorMessage = null;

    function __construct($presentationPartHtml) {
        $this->boundary = uniqid('', true);
        $this->appendPart("Presentation", "text/html", $presentationPartHtml);
    }

    public function appendPart($partName, $partContentType, $partContent) {
        $this->parts[] = "--{$this->boundary}\r\nContent-Disposition: form-data; name=\"$partName\"\r\nContent-Type: $partContentType\r\n\r\n$partContent\r\n\r\n";
    }

    public function send($url, $accessToken) {
        $cUrl = curl_init();
        curl_setopt($cUrl, CURLOPT_URL, $url);
        curl_setopt($cUrl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($cUrl, CURLOPT_HEADER, false);
        curl_setopt($cUrl, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($cUrl, CURLOPT_SSL_VERIFYPEER, true);
        curl_setopt($cUrl, CURLOPT_VERBOSE, true);
        curl_setopt($cUrl, CURLOPT_POST, true);
        curl_setopt(
            $cUrl,
            CURLOPT_HTTPHEADER,
            array(
                "Content-Type: multipart/form-data; boundary={$this->boundary}",
                "Authorization: Bearer $accessToken"
            )
        );

        $postData = implode($this->parts) . "--{$this->boundary}--";

        curl_setopt($cUrl, CURLOPT_POSTFIELDS, $postData);        
        $response = curl_exec($cUrl);
        $this->responseInfo = curl_getinfo($cUrl);
        $this->errorMessage = curl_error($cUrl);
        curl_close($cUrl);

        return $response;
    }

    public function getResponseInfo() {
        return $this->responseInfo;
    }

    public function getErrorMessage() {
        return $this->errorMessage;
    }
}

$presentationPartHtml = <<<PRESENTATIONHTML
<!DOCTYPE html>
<html>
    <head>
    <title>Title of the captured OneNote page</title>
    <meta name="created" content="2013-06-11T12:45:00.000-8:00"/>
    </head>
    <body>
        <p>This is a simple multi-part HTML page.</p>
        <img src="name:image1"/><br/>
        <img src="name:image2"/><br/>
        <object data-attachment="Logo.png" data="name:image3" type="image/png"/>
    </body>
</html>
PRESENTATIONHTML;

$imageData = "iVBORw0KGgoAAAANSUhEUgAAAMMAAACACAYAAAC2s91oAAAAAXNSR0ICQMB9xQAAAAlwSFlzAAAXEgAAFxIBZ5/SUgAAABl0RVh0U29mdHdhcmUATWljcm9zb2Z0IE9mZmljZX/tNXEAABJzSURBVHja7Z2JV1R3ssfnj5nzJntiNAnGOMlLJiaZyTpnkkky2SbvZfIymRiRpdlBFtkUkF0UEALuiqIowQ0VV3DBJSriAij7KsiO9e63mtuCSnfTcLtvN1WeOqgHsbvv7/Nbqr6/qt+RmOZ2t6OHmq83UdXRK1Sx7SQVryiifI9cSvwkjsJfDabQVwIp6AUf8nvWk3ye9iD/WV4UOMeHfJ5aTGv+k0lDg0PyIdrBficfwfTZyNAIdTV10o1T1+mUMuh3LS+kDT75lPFtKsV9EEMBsw1keHIxD3r/Wd7850AFgqAXfSn4JT8KcfOnkLn33fcZD4FBYNC3DfQOUFtdK926WEcX9p6jg2tKab0y6FM+T6TYPy+liNeXKAPbT5nZPYyD/zkvHvg84Of6W+0Cg8CgK+vv6aeaypt0/tdKKs3cT4VLCyh3YRbFfxijDNgAZZb34lne/3mjj53tH5zpJ+sCg8BgdxseHKbutm5qrWmh6hNVVJZ3iAojCyjzXysp8eM4inozjJa8HMD7ee/H3clX2e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Fp+hvam/8nkk8/sMWvHxcg4fckMSZZuFyJZ6FhkX/hUYBAZXNkS2cB65Xn6NGyceyNzHravSv0rm5oWIrau1TxmUZz35AK/VSiIwCAy6MjQ17O/p5+1WzdmbdHLLcdodt5NDnmlfJhlzJMpWiw/sTy02AjLbMC1JRIFBYHAa67vTR43VDXSp9CKd2HiMipYV0jqvPEr8JI7C/hjM4V8c0tWGi2oS0dqVRGAQGJzaENWCPKHlZjNdOniRDueWsmQh49tULqGCOkIY6IYn3dn5TDLHYMyNuI1XhAoMAoPLWndrN904dY0qd5+hAxl7aWvoJsr810ouoRL8kq9p9eB+DIp7PfYzZf2wSmAQGGaGcRKxtpXqLtTyJR5k2tFFJ/nTBBbn5S3OERgEhpltOLi31LRQ/eXb/HsxgUFMzG72/5T2MiEABwKaAAAAAElFTkSuQmCC";

$oneNoteMultiPartRequest = new OneNoteMultiPartRequest($presentationPartHtml);
$oneNoteMultiPartRequest->appendPart("image1", "image/png", base64_decode($imageData));
$oneNoteMultiPartRequest->appendPart("image2", "image/png", base64_decode($imageData));
$oneNoteMultiPartRequest->appendPart("image3", "image/png", base64_decode($imageData));
$response = $oneNoteMultiPartRequest->send("https://www.onenote.com/api/v1.0/pages", "<YOUR ACCESS TOKEN>");

var_dump($response);
echo '<br/><br/>';

$info = $oneNoteMultiPartRequest->getResponseInfo();
var_dump($info);
echo '<br/><br/>';

$errorMessage = $oneNoteMultiPartRequest->getErrorMessage();
var_dump($errorMessage);