phplaravelmicrosoft-graph-apionenote-api

OneNote error creating a page in Laravel using Microsoft Graph


i'm using Microsoft Graph in my Laravel application.
It works perfectly adding events to the user's calendar, getting events, creating OneNote's notebook.

The problem occurs when i try to add a page, i'm getting the next error:
enter image description here The code i'm using is:

public function createNewNote()
    {
        $graph = $this->getGraph();

        // Build the event
            $newEvent = [
                'body' => [
                    'content' => 'New page in default notebook',
                    'contentType' => 'text/html'
                ]
            ];
            // POST /me/onenote/notebooks DEFAULT NOTEBOOK
            $response = $graph->createRequest('POST', '/me/onenote/pages')
                ->attachBody($newEvent)
                ->setReturnType(Model\Notebook::class)
                ->execute();
            dd("Success");
//            return redirect('/calendar');
    }

The next code works fine:

public function createNewNotebook()
    {
        $graph = $this->getGraph();
        
            $newEvent = [
                'displayName' => 'Creating new notebook'
            ];
            // POST /me/onenote/notebooks DEFAULT NOTEBOOK
            $response = $graph->createRequest('POST', '/me/onenote/notebooks')
                ->attachBody($newEvent)
                ->setReturnType(Model\Notebook::class)
                ->execute();
            dd("Notebook Created");
//            return redirect('/calendar');
    }

I can't find the exact array or json structure for the body. What am i doing wrong? I also tried setting the text directly:
public function createNewNote()
    {
        $graph = $this->getGraph();
            // POST /me/onenote/notebooks DEFAULT NOTEBOOK
            $response = $graph->createRequest('POST', '/me/onenote/pages')
                ->attachBody('<div>Content</div>')
                ->setReturnType(Model\Notebook::class)
                ->execute();
            dd("Success");
//            return redirect('/calendar');
    }

And getting this error:
[![enter image description here][1]][2]
As i said i can create events (calendar) and notebooks (onenote) but i can't add a page with simple html or text.

Thank you so much for your help!


Solution

  • As specified in the document since you are putting the HTML data in attachBody directly without specifying the content type.

    That's the reason the other API calls work as they have JSON data specified properly.

    As specified here in the example they have specified the content type.

    I have tested the same with POSTMAN as shown below where you can see the content-type I have mentioned as text/html as the content we give is a div tag and it worked.

    enter image description here