none
Windows 8 app losing Twitter authentication RRS feed

  • Question

  • For some reason, our Windows 8 application is losing it's Twitter authorization. Our app works by having the user login with a Windows Live ID. Once logged in, the user can then link their Facebook and Twitter accounts. We are then storing the credentials in SQL Azure using Windows Azure Mobile Services. Every once in a while, the Twitter icon turns from blue to grey, which indicates that Twitter is not authorized. We then have to go back in and re-authorize Twitter to work. Any idea of what the problem might be? We do not see the same problem with Facebook. Again, we are storing the access tokens / credentials in Windows Auzre. Thanks, Brian
    Monday, June 17, 2013 6:11 PM

All replies

  • Hi Brian,

    How long is a Twitter auth good for (according to them)?  Do they require periodic refreshing?  Remember you are authorizing with them!

    -Jeff


    Jeff Sanders (MSFT)

    @jsandersrocks - Windows Store Developer Solutions @WSDevSol
    Getting Started With Windows Azure Mobile Services development? Click here
    Getting Started With Windows Phone or Store app development? Click here
    My Team Blog: Windows Store & Phone Developer Solutions
    My Blog: Http Client Protocol Issues (and other fun stuff I support)

    Monday, June 17, 2013 6:17 PM
  • I just found this. It looks like Twitter made a recent change. Would this cause a problem in the way Twitter authentication works in Windows Azure Mobile Services?

    https://dev.twitter.com/discussions/16443

    @Jeff, in answer to your question, the Twitter token should be good for a long time.

    Thanks,

    Brian

     
    Monday, June 17, 2013 6:27 PM
  • Hi Brian,

    I don't think it has any bearing, it is mostly addressing scraping the info from the stream.  The actual auth process happens by popping up a browser control to get the auth token.  How long is your Twitter token work?  Hours? Minutes, only once?  When did you notice the change?  Did this ever work or you are just noticing this in development?

    -Jeff


    Jeff Sanders (MSFT)

    @jsandersrocks - Windows Store Developer Solutions @WSDevSol
    Getting Started With Windows Azure Mobile Services development? Click here
    Getting Started With Windows Phone or Store app development? Click here
    My Team Blog: Windows Store & Phone Developer Solutions
    My Blog: Http Client Protocol Issues (and other fun stuff I support)

    Monday, June 17, 2013 6:39 PM
  • The application requests a token per user and stores the token in Azure. The application then uses the same token over and over to request access on the user's behalf. We are currently in development and trying to release the application to the Windows 8 app store (this is the last remaining issue). I believe that the problem has always been there. In the past, the problem would probably cause the app to crash and close. Now that we have error handling around this event, the icon just goes from blue to grey and we have to re-authenticate the app to Twitter. Is there a way to log errors for a Windows 8 app using Windows Azure Mobile Services? As is, we don't have any visibility into the problem. Brian
    Monday, June 17, 2013 6:49 PM
  • Hey Brian,

    I got ya.  I have not personally tried to store the Twitter Token so not sure how that would work but I will ping the team to see if anyone else has experience in that realm.  You could log errors to your Mobile Service by having a table to write to that is not secured using the user.id, but You would not be able to tie that to a particular user if you did not log in successfully (not sure if that would be useful to you).

    -Jeff


    Jeff Sanders (MSFT)

    @jsandersrocks - Windows Store Developer Solutions @WSDevSol
    Getting Started With Windows Azure Mobile Services development? Click here
    Getting Started With Windows Phone or Store app development? Click here
    My Team Blog: Windows Store & Phone Developer Solutions
    My Blog: Http Client Protocol Issues (and other fun stuff I support)

    Monday, June 17, 2013 6:55 PM
  • Brian,

    Check out Josh's post:

    http://www.thejoyofcode.com/Handling_expired_tokens_in_your_application_Day_11_.aspx

    -Jeff


    Jeff Sanders (MSFT)

    @jsandersrocks - Windows Store Developer Solutions @WSDevSol
    Getting Started With Windows Azure Mobile Services development? Click here
    Getting Started With Windows Phone or Store app development? Click here
    My Team Blog: Windows Store & Phone Developer Solutions
    My Blog: Http Client Protocol Issues (and other fun stuff I support)

    Monday, June 17, 2013 6:57 PM
  • On more thing,

    You are following our guidance and samples on Twitter authentication and not cooking up your own correct?  We don't provide a 'Twitter Icon' for example.

    -Jeff


    Jeff Sanders (MSFT)

    @jsandersrocks - Windows Store Developer Solutions @WSDevSol
    Getting Started With Windows Azure Mobile Services development? Click here
    Getting Started With Windows Phone or Store app development? Click here
    My Team Blog: Windows Store & Phone Developer Solutions
    My Blog: Http Client Protocol Issues (and other fun stuff I support)

    Monday, June 17, 2013 7:06 PM
  • We'll look into the expired token issue. I don't think that's it but, we'll see. Thanks, Brian
    Monday, June 17, 2013 7:23 PM
  • We have a Facebook icon and Twitter icon in our app that a user clicks on to start the authentication process. I believe that we are using your code.

    Regards, Brian

    Monday, June 17, 2013 7:25 PM
  • @Jeff, have you uncovered anymore helpful blog posts or information that might help us solve this issue? Brian
    Monday, June 17, 2013 9:05 PM
  • Hi Brian,

    Were you able to figure out what the issue was?  Have you made any progress?  If not, can you share a simplified repro of your problem that I can use to dig further into the issue?

    -Jeff


    Jeff Sanders (MSFT)

    @jsandersrocks - Windows Store Developer Solutions @WSDevSol
    Getting Started With Windows Azure Mobile Services development? Click here
    Getting Started With Windows Phone or Store app development? Click here
    My Team Blog: Windows Store & Phone Developer Solutions
    My Blog: Http Client Protocol Issues (and other fun stuff I support)

    Tuesday, June 18, 2013 12:03 PM
  • Hey Brian,

    I took some time to modify this sample to store and use the MobileService JWT that you get when authenticating with Twitter.

    It stores the ID and JWT in the PasswordVault (which is secure and local to the app).

            private MobileServiceCollection<TodoItem, TodoItem> items;
            private IMobileServiceTable<TodoItem> todoTable = App.MobileService.GetTable<TodoItem>();
    
            private async void Launch_Click(object sender, RoutedEventArgs e)
            {
                // PasswordVault is a secure place to store your password (in this case your JWT from Mobile Services)
                Windows.Security.Credentials.PasswordVault vault = new Windows.Security.Credentials.PasswordVault();
                
                PasswordCredential TwitterCred = null;
                try
                {
                    // See if we have stored Twitter credentials
                    IReadOnlyList<PasswordCredential> creds = vault.FindAllByResource("Twitter");
                    if (creds!=null)
                    {
                        // In my code there should ONLY be on Twitter cred so problably should check that.
                        TwitterCred = creds[0];
    
                        // Create a User to use with the Mobile Service (instead of doing the login sequence)
                        MobileServiceUser user = new MobileServiceUser(TwitterCred.UserName);
                        // Explicitly fetch the PWD into memory 
                        TwitterCred.RetrievePassword();
                        // Set the JWT 
                        user.MobileServiceAuthenticationToken = TwitterCred.Password;
                        // Assign it to the Current user.  Now it will be used to auth when retrieving stuff from the tables
                        App.MobileService.CurrentUser = user;
    
                        this.OutputPrint(string.Format("User set to - {0}", user.UserId));
                        // remove from memory
                        TwitterCred = null;
                    }
    
                }
                catch (Exception ex)
                {
                    // OK, you can get an exeption if the vault does not have this Twitter cred (not found)
                }
    
    
                try
                {
                    // If there is no CurrentUser set...
                    if (App.MobileService.CurrentUser == null)
                    {
                        MobileServiceUser user = await App.MobileService.LoginAsync(MobileServiceAuthenticationProvider.Twitter);
    
                        TwitterCred = new PasswordCredential("Twitter",user.UserId,user.MobileServiceAuthenticationToken);
                        vault.Add(TwitterCred );
                        TwitterCred = null;
    
                        this.OutputPrint(string.Format("You are now logged in - {0}", user.UserId));
                    }
                    
                    
                }
                catch (InvalidOperationException exLogin)
                {
                    this.DebugPrint("An error occurred during login. Login Required. Message: " + exLogin.Message );
                }
    
                
                try
                {
    
                    items = await todoTable.ToCollectionAsync();
                }
                catch (MobileServiceInvalidOperationException exMobileSvc)
                {
                    if (exMobileSvc.Response != null)
                    {
                        // See if this is an unauthorized status.  If so the JWT may have expired.
                        if (exMobileSvc.Response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                        {
                            // Set current user to null
                            App.MobileService.CurrentUser = null;
    
                            //Auth token could be expired so set the clear the vault of all Twitter creds and try again to get a new refreshed token
                            // NOTE:  This is not bess practice because this could throw an exception too.  Separate this out in your code to a function
                            IReadOnlyList<PasswordCredential> creds = vault.FindAllByResource("Twitter");
                            if (creds != null)
                            {
                                foreach (PasswordCredential theCred in creds)
                                {
                                    vault.Remove(theCred);
                                }
                            }
                
                        // TODO:  Reauthorize and try the fetch again...
                        }
                    }
                }
                catch(Exception)
                {
                    // some other exception so log it or display it or handle it some other way
                }
    
                vault = null;
            }
        }
    
        public class TodoItem
        {
            public int Id { get; set; }
    
            [JsonProperty(PropertyName = "text")]
            public string Text { get; set; }
    
            [JsonProperty(PropertyName = "complete")]
            public bool Complete { get; set; }
    
    
    
        }


    Jeff Sanders (MSFT)

    @jsandersrocks - Windows Store Developer Solutions @WSDevSol
    Getting Started With Windows Azure Mobile Services development? Click here
    Getting Started With Windows Phone or Store app development? Click here
    My Team Blog: Windows Store & Phone Developer Solutions
    My Blog: Http Client Protocol Issues (and other fun stuff I support)

    Tuesday, June 18, 2013 6:28 PM
  • We checked and this was not the problem. Brian
    Tuesday, June 18, 2013 6:31 PM
  • We will try this. Thanks ever so much!

    Brian

    Tuesday, June 18, 2013 6:33 PM
  • Brian,

    I put a quick POST up on my blog with a class I made to handle some of the maintenance:

    http://blogs.msdn.com/b/jpsanders/archive/2013/06/24/use-passwordvault-to-store-your-tokens-windows-azure-mobile-services.aspx

    Jeff


    Jeff Sanders (MSFT)

    @jsandersrocks - Windows Store Developer Solutions @WSDevSol
    Getting Started With Windows Azure Mobile Services development? Click here
    Getting Started With Windows Phone or Store app development? Click here
    My Team Blog: Windows Store & Phone Developer Solutions
    My Blog: Http Client Protocol Issues (and other fun stuff I support)

    Monday, June 24, 2013 6:25 PM