2017-07-24 70 views
1

我不能避免收到以下错误:无法发布到OneNote客户

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

      } 
+0

你在做什么平台? ASP.NET? Windows商店? –

回答

1

以下组合最后的作品:

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

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

这本来是比较容易,如果一些的github OneNote示例将被更新。

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

的直播SDK已经过时 - 你不应该使用它。我向你推荐以下内容:developer.microsoft.com/en-us/graph/code-samples-and-sdks Microsoft Graph SDK和示例跨越多个平台并使用最新支持的身份验证框架。有了这些,你可以调用POST〜/页面(甚至有SDK支持)

+0

这是获取令牌的代码。它是对MSAL_Authentication github代码的修改。 –

+0

将“令牌代码”添加到原始帖子中。 –

+0

您是否尝试发布我建议的端点? –

0

端点看起来不正确。应该既可以由豪尔赫提供以上,或者如果你直接打OneNote的API,它应该是:“https://www.onenote.com/api/v1.0/me/notes/pages

+0

添加您建议的EndPoint后,我仍然收到错误401。以下是我使用的一些EndPoints,并给出了错误401 @Jorge Aquirre。当我使用Live SDK时,我可以在没有问题的情况下发布到OneNote页面。只有在转换到Microsoft.Identity.Client时才有问题。以下是端点:'代码' –

+0

////设置API端点 //字符串_GraphAPIEndpoint =“https://www.onenote.com/api/v1.0/pages”; // string _GraphAPIEndpoint =“https://graph.microsoft.com/v1.0/me/onenote/pages”; string _GraphAPIEndpoint =“https://www.onenote.com/api/v1.0/me/notes/pages”; –

+0

你能分享401响应中的X-CorrelationId头吗? – Manjusha