Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C) OneNote - Create PageCreates a new OneNote page with a rendered image and an attached PDF. For more information, see https://docs.microsoft.com/en-us/graph/api/section-post-pages?view=graph-rest-1.0
#include <C_CkHttpW.h> #include <C_CkHttpRequestW.h> #include <C_CkStringBuilderW.h> #include <C_CkDateTimeW.h> #include <C_CkBinDataW.h> #include <C_CkHttpResponseW.h> #include <C_CkJsonObjectW.h> void ChilkatSample(void) { HCkHttpW http; BOOL success; HCkHttpRequestW req; HCkStringBuilderW sbHtml; BOOL bCrlf; HCkDateTimeW dtNow; int numReplaced; HCkBinDataW bdPdf; HCkHttpResponseW resp; HCkStringBuilderW sbResponseBody; HCkJsonObjectW jResp; int respStatusCode; const wchar_t *odata_context; const wchar_t *id; const wchar_t *self; const wchar_t *createdDateTime; const wchar_t *title; const wchar_t *createdByAppId; const wchar_t *contentUrl; const wchar_t *lastModifiedDateTime; const wchar_t *linksOneNoteClientUrlHref; const wchar_t *linksOneNoteWebUrlHref; // This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. http = CkHttpW_Create(); // To create a OneNote page, we want to send an HTTP request like the following: // POST https://graph.microsoft.com/v1.0/me/onenote/sections/{section_id}/pages // Content-length: 312 // 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 from an online source:</p> // <img src="https://..." alt="an image on the page" width="500" /> // <p>Here's an image uploaded as binary data:</p> // <img src="name:imageBlock1" alt="an image on the page" width="300" /> // <p>Here's a file attachment:</p> // <object data-attachment="FileName.pdf" data="name:fileBlock1" type="application/pdf" /> // </body> // </html> // // --MyPartBoundary198374 // Content-Disposition:form-data; name="imageBlock1" // Content-Type:image/jpeg // // ... binary image data ... // // --MyPartBoundary198374 // Content-Disposition:form-data; name="fileBlock1" // Content-Type:application/pdf // // ... binary file data ... // // --MyPartBoundary198374-- // Build the request in a Chilkat HTTP request object: req = CkHttpRequestW_Create(); // Our URL is https://graph.microsoft.com/v1.0/me/onenote/sections/{section_id}/pages // The path part of the URL is "/v1.0/me/onenote/sections/{section_id}/pages" // In this example, our section ID is "0-3A33FCEB9B74CC15!20350" CkHttpRequestW_putPath(req,L"/v1.0/me/onenote/sections/0-3A33FCEB9B74CC15!20350/pages"); // We'll be sending a POST. CkHttpRequestW_putHttpVerb(req,L"POST"); // The Content-Type is multipart/form-data // Chilkat will automatically generate a boundary string. CkHttpRequestW_putContentType(req,L"multipart/form-data"); // When Chilkat HTTP was written many years ago, multipart requests were primarily for uploads. // Thus the names of methods that add a multipart section end with "ForUpload". // The multipart request we wish to build has 3 sections: text/html, image/jpeg, and application/pdf. // ------------------------------ // Add the text/html part. // ------------------------------ sbHtml = CkStringBuilderW_Create(); bCrlf = TRUE; CkStringBuilderW_AppendLine(sbHtml,L"<!DOCTYPE html>",bCrlf); CkStringBuilderW_AppendLine(sbHtml,L"<html>",bCrlf); CkStringBuilderW_AppendLine(sbHtml,L" <head>",bCrlf); CkStringBuilderW_AppendLine(sbHtml,L" <title>A page with <i>rendered</i> images and an <b>attached</b> file</title>",bCrlf); CkStringBuilderW_AppendLine(sbHtml,L" <meta name=\"created\" content=\"TIMESTAMP_CURRENT\" />",bCrlf); CkStringBuilderW_AppendLine(sbHtml,L" </head>",bCrlf); CkStringBuilderW_AppendLine(sbHtml,L" <body>",bCrlf); CkStringBuilderW_AppendLine(sbHtml,L" <p>Here's an image from an online source:</p>",bCrlf); CkStringBuilderW_AppendLine(sbHtml,L" <img src=\"https://www.chilkatsoft.com/images/starfish.jpg\" alt=\"an image on the page\" width=\"500\" />",bCrlf); CkStringBuilderW_AppendLine(sbHtml,L" <p>Here's an image uploaded as binary data:</p>",bCrlf); CkStringBuilderW_AppendLine(sbHtml,L" <img src=\"name:imageBlock1\" alt=\"an image on the page\" width=\"300\" />",bCrlf); CkStringBuilderW_AppendLine(sbHtml,L" <p>Here's a file attachment:</p>",bCrlf); CkStringBuilderW_AppendLine(sbHtml,L" <object data-attachment=\"FileName.pdf\" data=\"name:fileBlock1\" type=\"application/pdf\" />",bCrlf); CkStringBuilderW_AppendLine(sbHtml,L" </body>",bCrlf); CkStringBuilderW_AppendLine(sbHtml,L"</html>",bCrlf); dtNow = CkDateTimeW_Create(); CkDateTimeW_SetFromCurrentSystemTime(dtNow); numReplaced = CkStringBuilderW_Replace(sbHtml,L"TIMESTAMP_CURRENT",CkDateTimeW_getAsTimestamp(dtNow,TRUE)); CkHttpRequestW_AddStringForUpload2(req,L"Presentation",L"",CkStringBuilderW_getAsString(sbHtml),L"utf-8",L"text/html"); // ------------------------------ // Add the JPG image. // ------------------------------ success = CkHttpRequestW_AddFileForUpload2(req,L"imageBlock1",L"qa_data/jpg/penguins2.jpg",L"image/jpeg"); if (success == FALSE) { wprintf(L"%s\n",CkHttpRequestW_lastErrorText(req)); CkHttpW_Dispose(http); CkHttpRequestW_Dispose(req); CkStringBuilderW_Dispose(sbHtml); CkDateTimeW_Dispose(dtNow); return; } // ------------------------------ // Add the PDF attachment. // ------------------------------ bdPdf = CkBinDataW_Create(); success = CkBinDataW_LoadFile(bdPdf,L"qa_data/pdf/helloWorld.pdf"); if (success == FALSE) { wprintf(L"Failed to load PDF file.\n"); CkHttpW_Dispose(http); CkHttpRequestW_Dispose(req); CkStringBuilderW_Dispose(sbHtml); CkDateTimeW_Dispose(dtNow); CkBinDataW_Dispose(bdPdf); return; } success = CkHttpRequestW_AddBdForUpload(req,L"fileBlock1",L"FileName.pdf",bdPdf,L"application/pdf"); if (success == FALSE) { wprintf(L"%s\n",CkHttpRequestW_lastErrorText(req)); CkHttpW_Dispose(http); CkHttpRequestW_Dispose(req); CkStringBuilderW_Dispose(sbHtml); CkDateTimeW_Dispose(dtNow); CkBinDataW_Dispose(bdPdf); return; } // Adds the "Authorization: Bearer ACCESS_TOKEN" header. CkHttpW_putAuthToken(http,L"ACCESS_TOKEN"); // POST to https://graph.microsoft.com/v1.0/me/onenote/sections/{section_id}/pages // The path part of the URL is already specified in the req object. // We only need to specify the domain and the fact that we're doing "https" (SSL/TLS). resp = CkHttpW_SynchronousRequest(http,L"graph.microsoft.com",443,TRUE,req); if (CkHttpW_getLastMethodSuccess(http) == FALSE) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkHttpW_Dispose(http); CkHttpRequestW_Dispose(req); CkStringBuilderW_Dispose(sbHtml); CkDateTimeW_Dispose(dtNow); CkBinDataW_Dispose(bdPdf); return; } sbResponseBody = CkStringBuilderW_Create(); CkHttpResponseW_GetBodySb(resp,sbResponseBody); jResp = CkJsonObjectW_Create(); CkJsonObjectW_LoadSb(jResp,sbResponseBody); CkJsonObjectW_putEmitCompact(jResp,FALSE); wprintf(L"Response status code: %d\n",CkHttpResponseW_getStatusCode(resp)); wprintf(L"Response Body:\n"); wprintf(L"%s\n",CkJsonObjectW_emit(jResp)); respStatusCode = CkHttpResponseW_getStatusCode(resp); wprintf(L"Response Status Code = %d\n",respStatusCode); if (respStatusCode >= 400) { wprintf(L"Response Header:\n"); wprintf(L"%s\n",CkHttpResponseW_header(resp)); wprintf(L"Failed.\n"); CkHttpResponseW_Dispose(resp); CkHttpW_Dispose(http); CkHttpRequestW_Dispose(req); CkStringBuilderW_Dispose(sbHtml); CkDateTimeW_Dispose(dtNow); CkBinDataW_Dispose(bdPdf); CkStringBuilderW_Dispose(sbResponseBody); CkJsonObjectW_Dispose(jResp); return; } CkHttpResponseW_Dispose(resp); // Sample JSON response: // (Sample code for parsing the JSON response is shown below) // { // "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('admin%40chilkat.io')/onenote/sections('0-3A33FCEB9B74CC15%2120350')/pages/$entity", // "id": "0-18ac61117a664f7e946bcceaeebd6f57!36-3A33FCEB9B74CC15!20350", // "self": "https://graph.microsoft.com/v1.0/users/admin@chilkat.io/onenote/pages/0-18ac61117a664f7e946bcceaeebd6f57!36-3A33FCEB9B74CC15!20350", // "createdDateTime": "2020-10-22T19:02:12Z", // "title": "A page with rendered images and an attached file", // "createdByAppId": "WLID-00000000441C9990", // "contentUrl": "https://graph.microsoft.com/v1.0/users/admin@chilkat.io/onenote/pages/0-18ac61117a664f7e946bcceaeebd6f57!36-3A33FCEB9B74CC15!20350/content", // "lastModifiedDateTime": "2020-10-23T00:02:13.3254289Z", // "links": { // "oneNoteClientUrl": { // "href": "onenote:https://d.docs.live.net/3a33fceb9b74cc15/Documents/Testing%20Notebook/Ddd.one#A%20page%20with%20rendered%20images%20and%20an%20attached%20file§ion-id=9d78c221-486e-45f8-8355-1810e475f6c0&page-id=36cd1982-1ef1-4b11-b5a1-30b3dbc43d05&end" // }, // "oneNoteWebUrl": { // "href": "https://onedrive.live.com/redir.aspx?cid=3a33fceb9b74cc15&page=edit&resid=3A33FCEB9B74CC15!20344&parId=3A33FCEB9B74CC15!187&wd=target%28Ddd.one%7C9d78c221-486e-45f8-8355-1810e475f6c0%2FA%20page%20with%20rendered%20images%20and%20an%20attached%20file%7C36cd1982-1ef1-4b11-b5a1-30b3dbc43d05%2F%29" // } // } // } // Sample code for parsing the JSON response... // Use the following online tool to generate parsing code from sample JSON: // Generate Parsing Code from JSON // Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat. // See this example explaining how this memory should be used: const char * functions. odata_context = CkJsonObjectW_stringOf(jResp,L"\"@odata.context\""); id = CkJsonObjectW_stringOf(jResp,L"id"); self = CkJsonObjectW_stringOf(jResp,L"self"); createdDateTime = CkJsonObjectW_stringOf(jResp,L"createdDateTime"); title = CkJsonObjectW_stringOf(jResp,L"title"); createdByAppId = CkJsonObjectW_stringOf(jResp,L"createdByAppId"); contentUrl = CkJsonObjectW_stringOf(jResp,L"contentUrl"); lastModifiedDateTime = CkJsonObjectW_stringOf(jResp,L"lastModifiedDateTime"); linksOneNoteClientUrlHref = CkJsonObjectW_stringOf(jResp,L"links.oneNoteClientUrl.href"); linksOneNoteWebUrlHref = CkJsonObjectW_stringOf(jResp,L"links.oneNoteWebUrl.href"); CkHttpW_Dispose(http); CkHttpRequestW_Dispose(req); CkStringBuilderW_Dispose(sbHtml); CkDateTimeW_Dispose(dtNow); CkBinDataW_Dispose(bdPdf); CkStringBuilderW_Dispose(sbResponseBody); CkJsonObjectW_Dispose(jResp); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.