Sagui Itay - Unity Assets, software development and mobile games

Accessing OneDrive via .Net

TLDR: I created a new NuGet package/GitHub repository for a
library to allow you access to OneDrive via .Net.

Over the last few days, I’ve implemented a .Net Class library to provide .Net access to OneDrive. Although Microsoft provides its own SDK (which is found at http://msdn.microsoft.com/en-US/live/ff621310), that library is not very developer friendly – it’s hard to debug, and harder to extend. I’ve search for alternatives on the internet, but the only usable solution I’ve found was John Hoerr‘s
SkyNet project. Although useful as a start, I’ve encountered several issues with it:

  • It didn’t support Async patterns
  • It’s support for downloading/uploading file was limited (mostly because it relies on RestSharp)
  • It’s Object Model wasn’t complete (some objects and methods were missing)
  • It wasn’t updated in a while

I’ve decided to implement my own library. I’ve started with the SkyNet repository. The first thing I did was replace all RestSharp code with simpler HttpClient (from the Microsoft.Net.Http NuGet).

This meant that I had to implement a simplified version of the Request
object. Nothing too complex – just enough to allow me to organize the code in a sensible manner.That also forced me to modify all method to Async, which was great, since I planned to do that anyway. Then I’ve added a few models, such as Error and UserProfile, along with their relevant methods. After that, came some extendibility requirements. The code that I currently
work on (and which will use this Library) makes quite a few requests to OneDrive. I needed a way to throttle all these requests. For the moment, I implemented this by providing callers the ability to wrap the HttpMessageHandler that the HttpClient is using.

Thus I’m able to use a TimeSpanSemaphore to throttle all requests.

I’ve also decided to implement auto-token-refresh. I’m using OneDriveRestAPI in a long-running process. That means that the AccessToken returned by OneDrive might expire during my processing. Using a RefreshToken, the library automatically retrieve an updated AccessToken (as long as the user didn’t revoke permissions on OneDrive).You are of course welcome to send me any feedback, fork and repository, and send pull requests. I’ll try to keep this up-to-date as much as possible.