Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Page in OneNote using Java SDK v6.x #2083

Open
DMarf opened this issue Jul 15, 2024 · 3 comments
Open

Create Page in OneNote using Java SDK v6.x #2083

DMarf opened this issue Jul 15, 2024 · 3 comments
Labels
area:serialization Focused on functional modules of the product type:bug A broken experience

Comments

@DMarf
Copy link

DMarf commented Jul 15, 2024

My current app successfully creates pages in OneNote using the Java SDK 5.x. I am looking to update to the latest Java SDK 6.x (in the process of also adding support for OneDrive).

I have read through the Microsoft Graph Java SDK v6 Changelog and Upgrade Guide and am obtaining valid tokens. I can't find, however, any documentation or examples of how to create a page in OneNote using the Java SDK 6.x. The article Create onenotePage provides examples for HTTP, CLI, and JavaScript, but is missing the examples for C#, Go, Java, etc. provided for other actions such as Create notebook.

Is it possible to create pages using the Java SDK 6.x? Are there any examples?

I have tried the Kotlin code below in the absence of documentation, but always receive the error "The multi-part payload was malformed". Debugging by enabling okhttp logging leads me to believe that this approach results in posting a serialized version of the OnenotePage object rather than the contents field as required. (I didn't provide my initialization for 'contents' as the whole approach seems invalid.)

val mypage = OnenotePage()
mypage.content = content

mGraphClient!!.me().onenote()
    .pages()
    .withUrl("https://graph.microsoft.com/v1.0/me/onenote/pages?sectionName=MySection")
    .post(mypage) { requestConfiguration ->
        requestConfiguration.headers.add(
            "content-type",
            "multipart/form-data; boundary=MyBoundary1234"
        )
    }
@DMarf DMarf added the status:waiting-for-triage An issue that is yet to be reviewed or assigned label Jul 15, 2024
@Ndiritu
Copy link
Contributor

Ndiritu commented Aug 7, 2024

Thanks for reaching out @DMarf.

The snippet you have is the best we can support now since we're not currently generating query parameters for POST requests.

From a high level check, the SDK writes the value of the OnenotePage content field to a stream directly.

Would you mind clarifying what you mean by the serialization approach being invalid?
Whether the error message you specified come from the API?
Whether your page content contains the boundary?

@Ndiritu Ndiritu added area:serialization Focused on functional modules of the product and removed status:waiting-for-triage An issue that is yet to be reviewed or assigned labels Aug 7, 2024
@DMarf
Copy link
Author

DMarf commented Aug 26, 2024

Thank you for following up @Ndiritu

When I enable okhttp logging it shows my post with the following body:
{"@odata.type":"#microsoft.graph.onenotePage","content":"{My Content Here}"}

The okhttp logging shows the following response to that post from the server:
<-- 400 https://graph.microsoft.com/v1.0/me/onenote/pages?sectionName=MySection
...
{"error":{"code":"20011","message":"The multi-part payload was malformed.","innerError":{"date":"2024-08-26T13:58:55","request-id":"17e87792-ff29-4d74-821f-4551a621730b","client-request-id":"8f00a619-c815-4524-8ea3-5bf38cad5253"}}}
<-- END HTTP (231-byte body)

I can confirm that my page content does contain the boundary.

I have been able to work around the problem using the following code with the same content that fails above as follows:

val mypage = OnenotePage()

val requestInformation = mGraphClient!!.me().onenote()
    .withUrl("https://graph.microsoft.com/v1.0/me/onenote/pages?sectionName=MySection")
    .pages()
    .toPostRequestInformation(mypage) { requestConfiguration ->
        requestConfiguration.headers.add(
            "Content-type",
            "multipart/form-data; boundary=MyBoundary1234"
        )
    }

requestInformation.content = content.inputStream()

val result = mGraphClient!!.requestAdapter.sendPrimitive(
    requestInformation,
    null,
    InputStream::class.java
)

With this code the okhttp logging shows my content being sent directly in the body of the post rather then being included in the "content" field of a serialized version of OnennotePage and my content successfully appears in OneNote. Is this the proper way the SDK is meant to be used?

By the approach being invalid, I meant that I wasn't sure if a page could be uploaded in the manner I expected. The SDK posts something else (the serialized page) versus what is required by the server (the page contents directly).

@Ndiritu
Copy link
Contributor

Ndiritu commented Sep 3, 2024

Thank you for clarifying @DMarf. Yes, this points to an issue in the SDK. Glad you were able to find a workaround.

@Ndiritu Ndiritu added the type:bug A broken experience label Sep 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:serialization Focused on functional modules of the product type:bug A broken experience
Projects
None yet
Development

No branches or pull requests

2 participants