无法 POST 到 OneNote 客户端

Cannot POST to OneNote Client

我无法避免收到以下错误:

StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:

这是我的代码:

public async Task<string> SendPostRequest(string _AccessToken)
    {
        HttpClient httpClient = new HttpClient();
        httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + _AccessToken);

        string date = GetDate();
        string simpleHtml = "<html>" +
                            "<head>" +
                            "<title>A simple page created from basic HTML-formatted text</title>" +
                            "<meta name=\"created\" content=\"" + date + "\" />" +
                            "</head>" +
                            "<body>" +
                            "<p>This is a page that just contains some simple <i>formatted</i> <b>text</b></p>" +
                            "<p>Here is a <a href=\"http://www.microsoft.com\">link</a></p>" +
                            "</body>" +
                            "</html>";

        var createMessage = new HttpRequestMessage(HttpMethod.Post, _GraphAPIEndpoint)
        {
            Content = new StringContent(simpleHtml, Encoding.UTF8, "text/html")
        };

        HttpResponseMessage response = await httpClient.SendAsync(createMessage);

        return response.ToString();


    }

这是 GraphEndpoint:

private string _GraphAPIEndpoint ="https://www.onenote.com/api/v1.0/pages";

登录和身份验证一切正常。我得到一个令牌等

这是获取我的令牌的代码:

 private const string _ClientID = "981c0157-69ec-4f42-8ec6-xxxxxxxxxxxxx";
       public static PublicClientApplication clientApplication = new PublicClientApplication(_ClientID);

       private AuthenticationResult authResult = null;

       authResult = await App.clientApplication.AcquireTokenAsync(_Scopes);
            if (authResult != null)
            {
               string response = await SendPostRequest(authResult.AccessToken);

            }

您是如何获得令牌的?

如果您通过 login.microsoftonline.com 获取令牌,请尝试发布到

https://graph.microsoft.com/v1.0/me/onenote/pages

您似乎在使用 C# - 我建议您查看我们拥有的示例,并将它们用作参考。

https://github.com/OneNoteDev/MsGraph_OneNoteApiSampleAspNetCore https://github.com/OneNoteDev/OneNoteAPISampleWinUniversal

Live SDK 已弃用 - 您不应使用它。那么我为您推荐以下内容:developer.microsoft.com/en-us/graph/code-samples-and-sdks Microsoft Graph SDK 和示例跨越许多平台并使用最新的、受支持的身份验证框架。有了这些,你可以调用 POST ~/pages (甚至有 SDK 支持)

端点看起来不正确。它应该是上面 Jorge 提供的,或者如果你直接点击 OneNote API,它应该是:“https://www.onenote.com/api/v1.0/me/notes/pages

以下组合最终有效:

 private string graphAPIEndpoint = "https://graph.microsoft.com/v1.0/me/onenote/pages";

    private string[] scopes = new string[] { "Notes.ReadWrite" };

如果一些 github OneNote 示例已经更新,那就更容易了。