如何使用OneNote API创建新的NoteBook [英] How to create new NoteBook with OneNote API

查看:187
本文介绍了如何使用OneNote API创建新的NoteBook的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试创建一个新的笔记本时,出现错误请求"错误400.以下是我的代码,我认为它是PagesEndPoint Uri,但我尝试了所有组合.我可以使用apigee控制台应用程序,但不能确定如何制作C#Windows应用程序的Post消息.

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);


        }

我已经尝试过使用此新代码,但是仍然无法正常工作.文档的链接显示了要使用的元素,但没有显示如何使用它们来制作C#方法.

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.

这是我的最新代码.

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);

        }

推荐答案

上面粘贴的最新代码有些错误. 这是我正在使用的修改后的版本:

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);
    }

请注意:

  • 我没有结合使用HttpClient和HttpWebRequest.
  • 在创建HttpWebRequest.Content时,我将mediaType设置为"application/json"
  • client.SendAsync()也使用HttpRequestMessage而不是postData字符串.

这篇关于如何使用OneNote API创建新的NoteBook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆