Office 365 API Tools August 2014 Update and RedirectRequiredException

05/08/2014

It’s really nice to see the cadence Microsoft is getting up to with their release cycle. Yesterday a new version of the Office 365 API Tools (1.1.728) was release that improves on the previous releases (not so much if you are using it in a web application, it seems like most improvements are for the client libraries and for phonegap).

Nevertheless I just took it for a quick spin in a MVC application as I have done with with the previous releases too. The first thing you will notice, if you try to add e.g. the MailApiSample to a simple MVC list view controller, is that you get an exception like this

[RedirectRequiredException: The browser must be redirected to a new URL.]
Microsoft.Office365.OAuth.DiscoveryContext`1.RedirectForResourceAuthorizationCode(String resourceId)

What the eck, they changed how the auth-flow is working. I’m not sure I agree with this change of using Exceptions to handle the typical Auth-flow, but I will have to spend some more time investigating what else have been changed before I make my verdict.

Anyways in order to correctly sign in you have to wrap your call to the MailApiSample (or MyFiles) in a try/catch and handle the RedirectRequiredException so my simple mail controller looks like this.

[csharp]
using Microsoft.Office365.OAuth;
using System.Threading.Tasks;
using System.Web.Mvc;

public class MailController : Controller
{
// GET: Mail
public async Task<ActionResult> Index()
{
try
{
var messages = await MailApiSample.GetMessages();
return View(messages);
}
catch (RedirectRequiredException ex)
{
return Redirect(ex.RedirectUri.ToString());
}

}
}
[/csharp]

If you want to read more check Chakkaradeep Chandran’s blog post and the official annoucement. It sounds like we are closing in on a non-preview version, I’m looking forward to that!