OneNote API Create Notebook



我在尝试创建新的 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
}}
  1. 您应该使用内容类型 JSON
  2. 您要查找的属性的名称不是"名称",而是"displayName">

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

最新更新