running the authorization synchronously in WinRt

Jan 25, 2013 at 1:36 AM
Edited Jan 25, 2013 at 1:38 AM

I am basically trying to run my authorization process for my WinRt app synchronously, but everything I've tried appears to deadlock the app after authorization on the MainPage, preventing navigation to the HomePage.  Can't seem to figure it out.  Here is my code without modifications, which works perfectly fine asynchronously below.  How can I make this work synchronously:

 

Class:

 

public async void InitializeTwitterAsnc()
        {

            WinRtAuthorizer authorizer = null;

            authorizer = new WinRtAuthorizer()
            {
                Credentials = new LocalDataCredentials()
                {
                    ConsumerKey = "blah379",
                    ConsumerSecret = "blah123"
                },
                UseCompression = true,
                Callback = new Uri("http://blah.com")
            };

            if(!authorizer.IsAuthorized)
            {
                await authorizer.AuthorizeAsync();
            }

            // set the twitter credential fields
            await Task.Run
                (() =>
                     {
                         twitterName = authorizer.Credentials.ScreenName;
                         twitterId = authorizer.Credentials.UserId;
                         accessToken = authorizer.Credentials.AccessToken;
                         oAuthToken = authorizer.Credentials.OAuthToken;

                         this.TwitterContext = new TwitterContext(authorizer);
                     });
        }

 

code that calls the method from MainPage.xaml.cs:

 

private void StartTwitterLogin(object sender, RoutedEventArgs e)
        {
            // start the twitter authorization
            TwitterAuth twitAuth = new TwitterAuth();
            twitAuth.InitializeTwitterAsnc();
           

            this.Frame.Navigate(typeof (HomePage));
        }
Coordinator
Jan 25, 2013 at 4:52 AM

Hi,

The WinRtAuthorizer is based on WebAuthenticationBroker, which is async. So, it isn't possible to perform a synchronous authentication with it.

The only synchronous option available is PinAuthorizer with InMemoryCredentials. Of course, you could write your own IOAuthCredentials type to replace InMemoryCredentials. Don't use any of the WinRtCredentials types with PinAuthorizer because they're async, which will cause a race condition that will break the authorization process.

I'm curious what is your use case for synchronous authorization in Windows 8?

@JoeMayo

Jan 25, 2013 at 5:27 AM

Well, I simply just don't want to navigate to the HomePage until the user has finished authorizing.  As it is now, the app navigates to the HomePage in the background while the user is still in the process of authorizing.  Initially I was thinking I could just do some sort of Wait() method, but that may or may not be likely.  Any ideas?

 

Rich

Coordinator
Jan 25, 2013 at 7:48 PM

Rich,

I see what's happening because as soon as you await in InitializeTwitterAsync(), control returns to the caller and you navigate before everything is done. Instead, do the navigation after the await on authorizer.AuthorizeAsync() inside of the InitializeTwitterLAsync() method.

@JoeMayo

Jan 25, 2013 at 11:27 PM

Joe, 

Yeah that solution was in the back of my mind, but being that I am new to working with async, I wasn't sure if there was a better way of resolving this issue with async or not.  Thanks for your time and and for a great product in linqtotwitter.

 

Rich