WinJS – AuthenticateAndContinue and “remote procedure call failed” exception

A while ago I started playing around a bit with JavaScript on Windows Phone. I wanted to create very simple app using AngularJS but of course I also had to use some WinJS features (for integration with device ). One of the things I needed to implement was authentication/authorization using OAuth. According to WinJS documentation it should be pretty straightforward – just use Windows.Security.Authentication.Web.WebAuthenticationBroker.authenticateAndContinue method with appropriate URLs. Of course I wanted authentication/authorization to be called before navigating to any screen, so I slightly modified default WinJS template and my code looked like that

Unfortunately all I got was this strange exception
remote procedure call failed

The exception itself originated somewhere from WinJS framework so I wasn’t able to debug it at all. It took me a while to figure this out but after all it turned out, that all you have to do, to make it work is to “schedule” call of AuthenticateAndContinue method using WinJS.Utilities.Scheduler

After this change entire code listing looks like that

After this procedure we should be successfully redirected to the requestUrl, in my case to Facebook login page
Succesfull

WinJS – AuthenticateAndContinue and “remote procedure call failed” exception