The OneNote API supports capturing images on to pages by including the image data in a POST. It requires a named <img>
tag in the "Presentation" block of a multi-part request and binary image data in a part having the name from the <img>
tag.
I cannot figure out how to embed the image data in the HTML body for the Invoke-RestMethod
cmdlet such that the API renders the image on to the OneNote page. Who can help?
The closest I can get to an undistorted image is by following the "Content-type:image/jpeg"
declaration by a blank line and by reading the image data UTF7-encoded from file.
$html = @"
--BlockBoundary
Content-Disposition:form-data; name="Presentation"
Content-type:text/html
<!DOCTYPE html>
<html>
<head><title>Page $Counter</title></head>
<body><img src="name:TheImage"/></body>
</html>
--BlockBoundary
Content-Disposition:form-data; name="TheImage"
Content-type:image/jpeg
$( Get-Content 'Image.jpg' -Raw -Encoding UTF7 )
--BlockBoundary--
"@
Invoke-RestMethod -Method Post `
-Uri 'https://www.onenote.com/api/v1.0/pages' `
-Headers @{"Authorization" = "Bearer " + $AccessToken} `
-ContentType 'multipart/form-data; boundary=BlockBoundary' `
-Body $html
The code snippet assumes Powershell 3.0 or higher, a valid access token being stored in the variable $AccessToken
and an accessible image file.