This is not my first post about the Office 365 API tools extension for Visual Studio. But in this post I want to highlight how to fix a few bugs that are present in the current version 1.1.512.
The content in this post was part of my preperation for a presentation I did at my work of Office 365 API tools, if you are interested in the slide deck, you can find it here: http://slides.com/simonj-k-pedersen/office-365-api-tools#
Exception when Querying OneDrive
When you query the OneDrive with the sample code provided when you enable MyFiles for your project, you will see the following exception:
The property 'Puid' does not exist on type 'Microsoft.Office365.SharePoint.UserInformation'. Make sure to only use property names that are defined by the type.
This happens because the OData client used by the library is configured to throw exceptions if not all properties returned from the OData source are mapped to strongly typed class that you are querying for. To fix it use the following code
var client = await EnsureClientCreated();
client.Context.IgnoreMissingProperties = true; //Must be set to avoid exception
The team behind the Office 365 API tools have on the MSDN forums promised to fix this in the next version.
Exception when I Access call AuthenticateAsync for the second time
When you try to call authentictator.AuthenticateAsync for the second time, the Office 365 API Tools framework is supposed to lookup the Access token from its in-memory cache. If this fails with the following exception
System.ArgumentOutOfRangeException: The UTC time represented when the offset is applied must be between year 0 and 10,000
then you see the same error as I do. I did some reflecting of the code in the framework, and to me it looks like there’s some errors in the current implementation of the cache they use for storing e.g. Access tokens. The first thing that caught my eye was inconsistent use of DateTime.Now and DateTime.UtcNow when setting the expirations time of cache items. But also the use of DateTime.MinValue seems to be causing some problems.
My fix to these problems was to create my own cache. The code for it (together with a sample project) is uploaded here: https://github.com/sjkp/Office365APITools/blob/master/Office365APIToolsSample/FixedSessionCache.cs
In order to use the error free cache in your project, you need to create the Authenticator the following way.
var authenticator = new Authenticator<FixedSessionCache>();
Also in order for the redirect URL that is implemented by a httphandler to work correctly with the new cache, you can no nolonger use the httphandler provided by the framework. To write your own, use this code
public class OAuth2RedirectHandler : OAuth2RedirectHandler<FixedSessionCache>, IRequiresSessionState
Finally you need to register your new httphandler inplace of the one that comes with the library. You do so in your web.config, for my sample project the configuration looks like this.
<add name="OAuth2RedirectHandler" verb="GET" path="/c5bee8be-77e8-41bc-a0b7-f901b00f3dd6.axd" type="Office365APIToolsSample.OAuth2RedirectHandler, Office365APIToolsSample, Version=126.96.36.199, Culture=neutral" />
With these two fixes you should be error free, at least when working with the files in OneDrive, I have yet to test all the other features of the library.
This is a great analysis and a very clever workaround for the time zone bug!