简体   繁体   English

为什么我不能向Microsoft OneNote发送multipart / form-data请求?

[英]Why can't I send a multipart/form-data request to Microsoft OneNote?

I am modifying software to export client data to Microsoft OneNote instead of to local html files. 我正在修改软件以将客户端数据导出到Microsoft OneNote而不是本地html文件。 I'm also not an experienced programmer, so I've been trying to teach myself this API and these protocols as I go along. 也不是一个经验丰富的程序员,所以我一直在尝试自学这个API和这些协议。

I am able to sucessfully use both the Apigee interface and hurl.it to send multipart POST requests and upload pages to a OneNote Notebook. 我能够成功使用Apigee接口hurl.it发送多部分POST请求并将页面上传到OneNote笔记本。

On hurl.it, I include two headers: 在hurl.it上,我包含两个标题:

"Authorization", "myAuthCode" “授权”,“myAuthCode”

"Content-Type", "multipart/form-data; boundary=NewPart" “Content-Type”,“multipart / form-data; boundary = NewPart”

While these interfaces work fine, I am unable to replicate the process in my Java project. 虽然这些接口工作正常,但我无法在Java项目中复制该过程。

Here is my test code: 这是我的测试代码:

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.MediaType;

public class Main {


public static void main(String[] args) {

    String tokenString = "LONG_TOKEN_STRING"

    Client client = ClientBuilder.newClient();
    Entity<String> payload = Entity.text("--NewPart\n" +
            "Content-Disposition: form-data; name=\"Presentation\"\n" +
            "Content-Type: application/xhtml+xml\n" +
            "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" +
            "<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en-us\">\n" +
            "  <head>\n" +
            ... //the rest of the POST request body is in here
            ...
            "</body></html>\n" +
            "--NewPart--\n" +
            ".\n");

    Response response = client.target("https://www.onenote.com/api/v1.0/pages")
            .request(MediaType.TEXT_PLAIN_TYPE)
            .header("Authorization", "Bearer " + tokenString)
            .header("Content-Type", "multipart/form-data; boundary=NewPart")
            .post(payload);

    System.out.println("status: " + response.getStatus());
    System.out.println("headers: " + response.getHeaders());
    System.out.println("body: \n" + response.readEntity(String.class));

    }
}

When I execute this code, I receive the following response: 当我执行此代码时,我收到以下响应:

"code":"20110","message":"Page create requests require the content to be multipart, with a presentation part." “code”:“20110”,“message”:“页面创建请求要求内容为多部分,并带有演示文稿部分。”

From this, I know that I'm successfully contacting OneNote, and successfully authenticating. 从此,我知道我已成功联系OneNote,并成功进行身份验证。

I believe that my error is in the way I set up the headers in Java. 我相信我的错误与我在Java中设置标头的方式有关。 I'm unsure if you're allowed to chain .header methods. 我不确定你是否被允许链接.header方法。 The only other way that I'm aware of is to pass a MultiValuedMap to the .headers method, though I'm unfamiliar with the interface and how to implement it. 我所知道的另一种方法是将MultiValuedMap传递给.headers方法,尽管我不熟悉界面以及如何实现它。

The OneNote Dev Center is a bit unhelpful, telling me only what I already know and seem to have included in my code. OneNote开发中心有点无益,只告诉我我已经知道的东西,似乎已经包含在我的代码中。

Edit: 编辑:

I've updated my code with CRLF 's in place of single \\n characters, though the problem persists: 虽然问题仍然存在,但我用CRLF代替单个\\ n字符更新了我的代码:

更新了Java代码OneNote错误代码

Look at Entity.text() 看看Entity.text()

Create a "text/plain" entity. 创建“text / plain”实体。

I haven't tested, but I'm guessing that this overwrites the Content-Type you set in the header() method. 我没有测试过,但我猜这会覆盖你在header()方法中设置的Content-Type You can use 您可以使用

Entity.entity(entity, MediaType)

to create a generic entity, where you can specify the media type. 创建通用实体,您可以在其中指定媒体类型。

Another thing, I don't know what JAX-RS implementation you are using, but any implementation should have multipart support, so you don't to manually handle the building of the body. 另一件事,我不知道你正在使用什么JAX-RS实现,但任何实现应该有多部分支持,所以你不要手动处理正文的构建。 Here is an example using Jersey . 以下是使用Jersey的示例

You should use CRLF \\r\\n instead of \\n [especially when dealing with ms/windows]. 您应该使用CRLF \\r\\n而不是\\n [尤其是在处理ms / windows时]。

It looks like you are missing a \\n character at the beginning of payload and a 2nd \\n after the \\n on the line "Content-Type: application/xhtml+xml\\n" 它看起来像你缺少一个\\n之初字符payload第2 \\n\\n就行了"Content-Type: application/xhtml+xml\\n"

sources: 来源:

http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html

https://www.ietf.org/rfc/rfc2046.txt https://www.ietf.org/rfc/rfc2046.txt

PS the rest of your code looks good PS其余代码看起来不错

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM