{"id":15,"date":"2015-02-19T22:11:00","date_gmt":"2015-02-19T22:11:00","guid":{"rendered":""},"modified":"2016-01-30T23:17:13","modified_gmt":"2016-01-30T23:17:13","slug":"winjs-authenticateandcontinue-and-remote-procedure-call-failed-exception","status":"publish","type":"post","link":"https:\/\/tpodolak.com\/blog\/2015\/02\/19\/winjs-authenticateandcontinue-and-remote-procedure-call-failed-exception\/","title":{"rendered":"WinJS &#8211; AuthenticateAndContinue and &#8220;remote procedure call failed&#8221; exception"},"content":{"rendered":"<p>A while ago I started playing around a bit with <i>JavaScript<\/i> on <i>Windows Phone<\/i>. I wanted to create very simple app using AngularJS but of course I also had to use some <i>WinJS<\/i> features (for integration with device ). One of the things I needed to implement was authentication\/authorization using <i>OAuth<\/i>. According to <i>WinJS<\/i> <a href=\"https:\/\/msdn.microsoft.com\/pl-pl\/library\/dn608114.aspx\"> documentation <\/a> it should be pretty straightforward &#8211; just use <i>Windows.Security.Authentication.Web.WebAuthenticationBroker.authenticateAndContinue<\/i> method with appropriate URLs. Of course I wanted authentication\/authorization to be called before navigating to any screen, so I slightly modified default <i>WinJS<\/i> template and my code looked like that<\/p>\n<pre lang=\"javascript\">\r\n\/\/ rest of code omitted for brevity\r\nvar activation = Windows.ApplicationModel.Activation,\r\n    app = WinJS.Application,\r\n    nav = WinJS.Navigation,\r\n    sched = WinJS.Utilities.Scheduler,\r\n    ui = WinJS.UI;\r\n\r\n app.addEventListener(\"activated\", function (args) {\r\n        if (args.detail.kind === activation.ActivationKind.launch) {\r\n            if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {\r\n                \/\/ TODO: This application has been newly launched. Initialize\r\n                \/\/ your application here.\r\n            } else {\r\n                \/\/ TODO: This application has been reactivated from suspension.\r\n                \/\/ Restore application state here.\r\n            }\r\n\r\n            nav.history = app.sessionState.history || {};\r\n            nav.history.current.initialPlaceholder = true;\r\n\r\n            var p = ui.processAll().then(function() {\r\n                Windows.Security.Authentication.Web.WebAuthenticationBroker.authenticateAndContinue(\r\n                        requestUrl, \/\/ for instance facebook auth url\r\n                        callbackUrl \/\/ for instance facebook auth callback url\r\n                    );\r\n            });\r\n            args.setPromise(p);\r\n        }\r\n    });\r\n\/\/ rest of code omitted for brevity\r\n<\/pre>\n<p>Unfortunately all I got was this strange exception<br \/>\n<a href=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2015\/02\/angularjs-extending-existing-services-using-decorator\/RemoteProcedureCallsFailed.png\" rel=\"attachment wp-att-278\"><img decoding=\"async\" src=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2015\/02\/angularjs-extending-existing-services-using-decorator\/RemoteProcedureCallsFailed.png\" alt=\"remote procedure call failed\" width=\"600\" class=\"aligncenter size-full wp-image-278\" srcset=\"https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2015\/02\/angularjs-extending-existing-services-using-decorator\/RemoteProcedureCallsFailed.png 752w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2015\/02\/angularjs-extending-existing-services-using-decorator\/RemoteProcedureCallsFailed-150x78.png 150w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2015\/02\/angularjs-extending-existing-services-using-decorator\/RemoteProcedureCallsFailed-300x156.png 300w\" sizes=\"(max-width: 752px) 100vw, 752px\" \/><\/a><\/p>\n<p>The exception itself originated somewhere from <i>WinJS<\/i> framework so I wasn&#8217;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 &#8220;schedule&#8221; call of <i>AuthenticateAndContinue<\/i> method using <i>WinJS.Utilities.Scheduler<\/i><\/p>\n<pre lang=\"javascript\">\r\nWinJS.Utilities.Scheduler.schedule(function(){ \/\/your code in here});\r\n<\/pre>\n<p>After this change entire code listing looks like that<\/p>\n<pre lang=\"javascript\">\r\n\/\/ rest of code omitted for brevity\r\nvar activation = Windows.ApplicationModel.Activation,\r\n   app = WinJS.Application,\r\n   nav = WinJS.Navigation,\r\n   sched = WinJS.Utilities.Scheduler,\r\n   ui = WinJS.UI;\r\n\r\napp.addEventListener(\"activated\", function (args) {\r\n       if (args.detail.kind === activation.ActivationKind.launch) {\r\n           if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {\r\n               \/\/ TODO: This application has been newly launched. Initialize\r\n               \/\/ your application here.\r\n           } else {\r\n               \/\/ TODO: This application has been reactivated from suspension.\r\n               \/\/ Restore application state here.\r\n           }\r\n           \r\n           nav.history = app.sessionState.history || {};\r\n           nav.history.current.initialPlaceholder = true;\r\n           \r\n           var p = ui.processAll().then(function() {\r\n               return sched.schedule(function() {\r\n                   Windows.Security.Authentication.Web.WebAuthenticationBroker.authenticateAndContinue(\r\n                       requestUrl, \/\/ for instance facebook auth url,\r\n                       callbackUrl \/\/ for instance facebook auth callback url\r\n                   );\r\n               });\r\n           });\r\n           \r\n           args.setPromise(p);\r\n       }\r\n   });\r\n\/\/ rest of code omitted for brevity\r\n<\/pre>\n<p>After this procedure we should be successfully redirected to the <i>requestUrl<\/i>, in my case to <i>Facebook<\/i> login page<br \/>\n<a href=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2015\/02\/angularjs-extending-existing-services-using-decorator\/Succesfull.png\" rel=\"attachment wp-att-279\"><img decoding=\"async\" src=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2015\/02\/angularjs-extending-existing-services-using-decorator\/Succesfull.png\" alt=\"Succesfull\" height=\"100px\" class=\"aligncenter size-full wp-image-279\" srcset=\"https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2015\/02\/angularjs-extending-existing-services-using-decorator\/Succesfull.png 476w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2015\/02\/angularjs-extending-existing-services-using-decorator\/Succesfull-90x150.png 90w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2015\/02\/angularjs-extending-existing-services-using-decorator\/Succesfull-179x300.png 179w\" sizes=\"(max-width: 476px) 100vw, 476px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32,31,18,29],"tags":[180,174,181],"class_list":["post-15","post","type-post","status-publish","format-standard","hentry","category-authenticateandcontinue","category-authorization","category-javascript","category-winjs","tag-authenticateandcontinue","tag-javascript","tag-winjs"],"_links":{"self":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/15","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/comments?post=15"}],"version-history":[{"count":5,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/15\/revisions"}],"predecessor-version":[{"id":528,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/15\/revisions\/528"}],"wp:attachment":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/media?parent=15"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/categories?post=15"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/tags?post=15"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}