簡體   English   中英

OneNote API創建筆記本

[英]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. 您應該使用Content-Type JSON
  2. 您要查找的屬性的名稱不是“名稱”,而是“ displayName”

此外,通過附加字符串來制作JSON並不是最佳實踐-我建議使用JSON庫,例如NewtonSoft JSON.NET。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM