【问题标题】:OneNote API Create NotebookOneNote API 创建笔记本
【发布时间】:2018-07-30 19:39:00
【问题描述】:

我在尝试创建新的 OneNote API 笔记本时收到“错误请求”。

 private async Task<string> CreateSimpleNotebook(string notebookName,  string apiRoute)
    {
        var client = new HttpClient();

          client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        try
        {
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
        }
        catch (Exception ex)
        {
            string tempEx = ex.ToString();
        }

        var createMessage = new HttpRequestMessage(HttpMethod.Post, apiRoute )
        {
            Content = new StringContent("{ name : '" + WebUtility.UrlEncode(notebookName) + "' }", Encoding.UTF8, "application/json")
        };

        HttpResponseMessage response = await client.SendAsync(createMessage);

        return response.Headers.Location.ToString();
    }

我使用以下方法调用该方法:

      string url = "https://graph.microsoft.com/v1.0/me/onenote/notebooks/";
        //   string url = "https://www.onenote.com/api/v1.0/me/notes/notebooks/";

        string tempResponse = await CreateSimpleNotebook("EMRTest2", url);

下面是回复:

    {StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  client-request-id: acacd4f5-8738-4c46-8150-17aa23413eb5
  request-id: acacd4f5-8738-4c46-8150-17aa23413eb5
  Transfer-Encoding: chunked
  x-ms-ags-diagnostic: {"ServerInfo":{"DataCenter":"South Central US","Slice":"SliceB","Ring":"NA","ScaleUnit":"002","Host":"AGSFE_IN_10","ADSiteName":"SAN"}}
  Duration: 772.4124
  Cache-Control: private
  Date: Sun, 19 Nov 2017 20:59:10 GMT
  Content-Type: application/json
}}

【问题讨论】:

    标签: c# onenote-api


    【解决方案1】:
    1. 您应该使用 Content-Type JSON
    2. 您要查找的属性名称不是“name”,而是“displayName”

    此外,通过附加字符串来制作 JSON 并不是最佳实践 - 我建议使用 JSON 库,例如 NewtonSoft JSON.NET。

    【讨论】:

    • 感谢“displayName”解决了这个问题。我看不出应该在哪里更改 Content-Type JSON。
    • 作为您请求的 HTTP 标头。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多