2017-08-28 1 views
0

J'utilise l'API Microsoft Graph et j'essaye de créer par programme une page oneNote à partir de C#.Les demandes de création de page requièrent multipart, avec la partie de présentation

public async Task<string> GetHttpContentWithToken(string url, string token) 
    { 
     var httpClient = new System.Net.Http.HttpClient(); 
     System.Net.Http.HttpResponseMessage response; 
     try 
     { 
      var request = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Post, url); 
      //Add the token in Authorization header 
      request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token); 

      byte[] byteArray = File.ReadAllBytes("C:\\Users\\t-aaalle\\Documents\\Visual Studio 2015\\Projects\\WordAddIn4\\WordAddIn4\\GreetingFile.docx"); 
      using (MemoryStream memoryStream = new MemoryStream()) 
      { 
       memoryStream.Write(byteArray, 0, byteArray.Length); 
       using (WordprocessingDocument doc = WordprocessingDocument.Open(memoryStream, true)) 
       { 
        HtmlConverterSettings settings = new HtmlConverterSettings() 
        { 
         PageTitle = "My Page Title" 
        }; 

        XElement html = HtmlConverter.ConvertToHtml(doc, settings); 
        var httpContent = new StringContent(html.ToString(), Encoding.UTF8, "multipart/form-data"); 
        string contentWord = httpContent.ToString(); 


        httpClient = new HttpClient(); 
        httpClient.DefaultRequestHeaders 
          .Accept 
          .Add(new MediaTypeWithQualityHeaderValue("text/html"));//ACCEPT header 

        request = new HttpRequestMessage(HttpMethod.Post, "https://graph.microsoft.com/v1.0/me/onenote/sections/1-48839a74-fc3c-452d-a8a8-1eb7d891bc58/pages"); 
        request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token); 
        request.Content = new StringContent("{\"name\":\"John Doe\",\"age\":33}",Encoding.UTF8, 
                 "application/json");//CONTENT-TYPE header 
        request.Headers.TryAddWithoutValidation("Content-Type", "multipart/form-data; boundary=----MyPartBoundary198374--"); 

        contentWord = html.ToString(); 

        var createMessage = new HttpRequestMessage(HttpMethod.Post, graphAPIEndpoint) 
        { 
         Content = new StringContent(contentWord, System.Text.Encoding.UTF8, "text/html") 
        }; 
       } 
      } 

      response = await httpClient.SendAsync(request); 
      var content = await response.Content.ReadAsStringAsync(); 
      return content; 
     } 
     catch (Exception ex) 
     { 
      return ex.ToString(); 
     } 
    } 

Je ne comprends pas ce que cela signifie quand je lance le programme et il jette l'erreur:

Page create requests require the content to be multipart, with a presentation part 

EDIT

Merci à Fei Xue qui a résolu ma question, et maintenant je Je reçois une nouvelle erreur qui indique ...

Write requests (excluding DELETE) must contain the Content-Type header declaration 

Le code mis à jour comprenant la réponse très perspicace est est ...

public async Task<string> GetHttpContentWithToken(string url, string token) 
{ 
    var httpClient = new System.Net.Http.HttpClient(); 
    System.Net.Http.HttpResponseMessage response; 
    try 
    { 
     var request = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Post, url); 
     //Add the token in Authorization header 
     request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token); 

     byte[] byteArray = File.ReadAllBytes("C:\\Users\\t-aaalle\\Documents\\Visual Studio 2015\\Projects\\WordAddIn4\\WordAddIn4\\GreetingFile.docx"); 
     using (MemoryStream memoryStream = new MemoryStream()) 
     { 
      memoryStream.Write(byteArray, 0, byteArray.Length); 
      using (WordprocessingDocument doc = WordprocessingDocument.Open(memoryStream, true)) 
      { 
       HtmlConverterSettings settings = new HtmlConverterSettings() 
       { 
        PageTitle = "My Page Title" 
       }; 

       XElement html = HtmlConverter.ConvertToHtml(doc, settings); 
       var httpContent = new StringContent(html.ToString(), Encoding.UTF8, "text/html"); 
       string contentWord = httpContent.ToString(); 

       using (var client = new HttpClient()) 
       { 
        client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token); 
        using (var content2 = new MultipartFormDataContent("MyPartBoundary198374")) 
        { 
         var stringContent = new StringContent("<h1>Hello</h1>", Encoding.UTF8, "text/html"); 
         content2.Add(stringContent, "Presentation"); 


         using (
          var message = await client.PostAsync("https://graph.microsoft.com/v1.0/me/onenote/sections/1-48839a74-fc3c-452d-a8a8-1eb7d891bc58/pages", content2)) 
         { 
          Console.WriteLine(message.StatusCode); 
         } 
        } 
       } 

       contentWord = html.ToString(); 
      } 
     } 

     response = await httpClient.SendAsync(request); 
     var content = await response.Content.ReadAsStringAsync(); 
     return content; 
    } 
    catch (Exception ex) 
    { 
     return ex.ToString(); 
    } 
} 

Merci beaucoup pour l'aide!

Répondre

0

Pour créer la page de OneNote, nous devons préciser la Présentation partie avec nom propriété dans le corps contenu. Voici un exemple qui fonctionne à l'aide Fiddler pour votre référence:

POST: https://graph.microsoft.com/v1.0/me/onenote/sections/{id}/pages 

authorization: bearer {token} 
Content-type: multipart/form-data; boundary=MyPartBoundary198374 

--MyPartBoundary198374 
Content-Disposition:form-data; name="Presentation" 
Content-Type:text/html 

<!DOCTYPE html> 
<html> 
    <head> 
    <title>A page with <i>rendered</i> images and an <b>attached</b> file</title> 
    <meta name="created" content="2015-07-22T09:00:00-08:00" /> 
    </head> 
    <body> 
    <p>Here's an image uploaded as binary data:</p> 
    <img src="name:imageBlock1" alt="an image on the page" width="300" /> 
    </body> 
</html> 
--MyPartBoundary198374 
Content-Disposition: form-data; name="imageBlock1" 
Content-Type: image/jpeg 

<@INCLUDE *C:\me.jpg*@> 
--MyPartBoundary198374-- 

Plus de détails sur cette demande, vous pouvez vous référer this link.

Et voici un exemple de code via C# pour créer une page HTML:

string token = ""; 
using (var client = new HttpClient()) 
{ 

    client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token); 
    using (var content = new MultipartFormDataContent("MyPartBoundary198374")) 
    { 
     var stringContent = new StringContent("<h1>Hello</h1>",Encoding.UTF8, "text/html"); 
     content.Add(stringContent, "Presentation"); 

     using (
      var message = 
       await client.PostAsync("https://graph.microsoft.com/v1.0/me/onenote/sections/{id}/pages", content)) 
     { 
      Console.WriteLine(message.StatusCode); 
     } 
    } 
} 
+0

MERCI BEAUCOUP –