无法POST到OneNote客户端

时间:2017-07-24 22:54:30

标签: onenote-api microsoft-graph

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

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

            }

3 个答案:

答案 0 :(得分:1)

您如何获得令牌?

如果您通过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支持)

答案 1 :(得分:1)

以下组合最终有效:

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

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

如果某些github OneNote样本会更新,那会更容易。

答案 2 :(得分:0)

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