John Liu .NET

View Original

Posting to Office 365 OneNote via PowerShell

This is a guide of links and gotchas of how to post a new page to your OneNote document sitting in Office 365.

Lots of this was learning for me, so thank you:

Background

Todd Klindt asked over the weekend if there's any examples of manipulating OneNote with PowerShell.

Now Todd is a master of PowerShell, which is NOT A DEVELOPER TOOL.  But I did recall OneNote has an ongoing REST API and recently announced they have it working on Office 365 (Preview).

http://blogs.msdn.com/b/onenotedev/archive/2015/04/30/support-for-work-and-school-notebooks-on-office-365-in-preview.aspx

And as a developer I thought I can at least try to write some PowerShell to get this working.  Besides, I want to play with the OAuth stuff and new APIs.

The LEGO pieces are in place, and the cogs in my head are churning.

To talk to Office 365 Unified API you need an Azure Application. 

Register it like this http://blogs.msdn.com/b/onenotedev/archive/2015/04/30/register-your-application-in-azure-ad.aspx

Here's mine:

These are the OneNote permissions you need

 

What you need:

  • ClientID
  • ClientSecret - copy this out if you lose it you'll have to make a new key
  • The Redirect URL

The Redirect URL is an interesting one.  In the case of a web application - this would be your provider hosted web application.  In PowerShell however, we don't actually _need_ a valid web application.  We just need the returned URL which contains the code. 

It has to be https though.  So https://localhost:12345 will work fine.  
In my picture you also see https://dev.office.com that's actually not necessary.  

Onto the PowerShell:

 

See this content in the original post

Note

This is a brilliant dialog.  WinForms, hosting a web control.  Genius.

 

See this content in the original post

With all the variables we needed, we can begin

See this content in the original post

This invokes the WinForm dialog above.

Haha!  WinForm in 2015 - love it!

See this content in the original post

You have an authCode - this is step 1.  You can now ask Office 365 for Access token and Refresh Tokens.

We don't need to do this from the browser window anymore.  Invoke-RestMethod cmd works great.

See this content in the original post

Well well!  What have we got here.

See this content in the original post

And finally...  Create Page

See this content in the original post

Beauty!

Summary of Gotchas

  • On this development environment I didn't have OneNote available in Azure App Permissions.  I found out I need to create a OneNote document in Office 365 first and open it with OneNote online.  That seems to register that I need OneNote API.
  • I then forgot to grant the app permissions for OneNote
  • I didn't UrlEncode the parameters
  • I also had trouble getting Unauthorized when talking to OneNote - because I was requesting the graph.microsoft.com resource instead of onenote.com
  • Sometimes I still get an occasional error in the browser dialog.  Re-running it it disappears. 
  • Also, the point of the refresh token is that you don't need to pop up the user dialog anymore after the first time.  The refresh token should be stored securely and used to request new access-token on subsequent runs.

Had a few head-scratching moments like this.  But it's all great fun.

Download