Exception when using WebAuthenticationBroker.AuthenticateAndContinue in OnNavigatedTo event

2

When using WebAuthenticationBroker.AuthenticateAndContinue in the OnNavigatedTo event of my MainPage I get the following exception:

The remote procedure call failed. (Exception from HRESULT: 0x800706BE)

However, if I create a button with a click event which does exactly the same function call, I don't get the exception. I think the reason is that the page isn't fully loaded when the OnNavigatedTo event is triggered.

So how should I go about creating a page that automatically starts the authorization process when I navigate to it, without having an intermediate control (like a button)?

c#
windows-phone-8.1
asked on Stack Overflow Apr 24, 2014 by Kinan Al Sarmini • edited Apr 24, 2014 by Marius Bancila

1 Answer

3

If I try to execute that token request function on the page Loaded event, I get the same error as you:

String FacebookURL = "https://www.facebook.com/dialog/oauth?client_id=" + Uri.EscapeDataString(FacebookClientID.Text) + "&redirect_uri=" + Uri.EscapeDataString(FacebookCallbackUrl.Text) + "&scope=read_stream&display=popup&response_type=token";

System.Uri StartUri = new Uri(FacebookURL);
System.Uri EndUri = new Uri(FacebookCallbackUrl.Text);

WebAuthenticationBroker.AuthenticateAndContinue(StartUri, EndUri, null, WebAuthenticationOptions.None);

The workaround I used to make this work involves a DispatcherTimer

    DispatcherTimer timer = new DispatcherTimer();
    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        timer.Interval = new TimeSpan(0, 0, 0);
        timer.Tick += timer_Tick;
        timer.Start();
    }
    void timer_Tick(object sender, object e)
    {
        timer.Stop();
        String FacebookURL = "https://www.facebook.com/dialog/oauth?client_id=" + Uri.EscapeDataString(FacebookClientID.Text) + "&redirect_uri=" + Uri.EscapeDataString(FacebookCallbackUrl.Text) + "&scope=read_stream&display=popup&response_type=token";
        System.Uri StartUri = new Uri(FacebookURL);
        System.Uri EndUri = new Uri(FacebookCallbackUrl.Text);
        WebAuthenticationBroker.AuthenticateAndContinue(StartUri, EndUri, null, WebAuthenticationOptions.None);            
    }
answered on Stack Overflow Apr 30, 2014 by user3591072 • edited Apr 30, 2014 by Brian Roach

User contributions licensed under CC BY-SA 3.0