How to create new NoteBook with OneNote API

倾然丶 夕夏残阳落幕 提交于 2019-12-11 19:53:51

问题


I'm getting a "Bad Request" error 400 when I try to create a new Notebook. Below is my code, I think it is the PagesEndPoint Uri but I have tried all combinations. I can use the apigee console app, but cannot detemine how to make a C# Windows app Post message.

async public Task<StandardResponse> CreateNewNotebook(string newNotebookName)
        {

            Uri PagesEndPoint = new Uri("https://www.onenote.com/api/v1.0/notebooks?notebookName="+newNotebookName.ToString());
            var client = new HttpClient();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            if (IsAuthenticated)
            {
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authClient.Session.AccessToken);
            }

            string date = GetDate();
            string simpleHtml = "<html>"+"<head>"+"<title>A simple page created with an image1 on it</title>" +"<meta name=\"created\" content=\"" + date + "\" />" +
                                "</head>" +"<body>" +"<h1>This is a page with an image on it</h1>" +"</body>" +"</html>";
            HttpResponseMessage response;
            HttpRequestMessage createMessage = new HttpRequestMessage(HttpMethod.Post, PagesEndPoint)
            {
                Content = new StringContent(simpleHtml, System.Text.Encoding.UTF8, "text/html")
            };

            response = await client.SendAsync(createMessage);
            tbResponse.Text = response.ToString();
            return await TranslateResponse(response);


        }

I've tried with this new code, but still not working. The links to the documentation show the elements to use, but not how to use them to make C# method.

Here is my latest code.

async public Task<StandardResponse> CreateJsonNotebook(string newNotebookName)
        {

            string postData = "{name: \"NewNotebookName\"}";

            var client = new HttpClient();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            if (IsAuthenticated)
            {
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authClient.Session.AccessToken);
            }

           StreamWriter requestWriter;
           var webRequest = System.Net.WebRequest.Create("https://www.onenote.com/api/v1.0/notebooks") as HttpWebRequest;

           HttpResponseMessage response;
           response = await client.SendAsync(postData);
           tbResponse.Text = response.ToString();
           return await TranslateResponse(response);

        }

回答1:


there are a few things incorrect with your latest code pasted above. Here's the modified version that I got working :

    public async Task<StandardResponse> CreateJsonNotebook(string newNotebookName)
    {
        var client = new HttpClient();
        string postData = "{name: \"" + newNotebookName + "\"}";
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        if (IsAuthenticated)
        {
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer",
                _authClient.Session.AccessToken);
        }

        StreamWriter requestWriter;
        var webRequest = new HttpRequestMessage(HttpMethod.Post, "https://www.onenote.com/api/v1.0/notebooks")
        {

            Content = new StringContent(postData, Encoding.UTF8, "application/json")
        };

        HttpResponseMessage response;
        response = await client.SendAsync(webRequest);
        return await TranslateResponse(response);
    }

Notice that:

  • I didn't combine usage of HttpClient and HttpWebRequest.
  • When creating the HttpWebRequest.Content, I set the mediaType to "application/json"
  • Also client.SendAsync() used the HttpRequestMessage and not the postData string.



回答2:


You're right - the URL isn't quite right. You can't actually create a page and a notebook at the same time - they require two different calls.

To create a notebook, the URL you should post to is:

https://www.onenote.com/api/v1.0/notebooks

The notebook is created with the content of the body, which should be JSON. (Make sure you include CONTENT-TYPE: application/json in the header).

The body should look like:

{
  name: "New Notebook Name"
}

You can then create a section in the notebook with the ID in the response. Once you get the ID of a new section, you can then post a page to that section.

More information can be found here: http://msdn.microsoft.com/en-us/library/office/dn790583(v=office.15).aspx




回答3:


The way you are calling the API is incorrect. You shouldn't be putting a notebookName query parameter in the endpoint. Instead, you should just post to https://www.onenote.com/api/v1.0/notebooks with a JSON body. The JSON body should be

{ name: "New notebook name" }

You can see this blog post for an example.

-- James



来源:https://stackoverflow.com/questions/25272434/how-to-create-new-notebook-with-onenote-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!