Troubles with authentication

Aug 4, 2010 at 5:03 PM


I just started to use LinqToTwitter yesterday.   The issue I am having is when my form starts up, it automatically is going to my personal twitter page.  I am assuming this is due to maybe having some cached credentials with windows 7?  I put auth.SignOff() in the form close method, but that did not change it.  I am using code snipits from the demo as shown below....


One thing that happened was with this line:


                auth = new UsernamePasswordAuthorization(Utilities.GetConsoleHWnd());

it first asked where the Utilities.cs was (when I first hit F5 to compile)...I browesed to that location and then it would give me an unhandled IO Exception in the Utilities.cs code.  I am assuming this was due to the fact that this is not a console app, but a windows form app..  I replaced that with the line below:

                auth = new UsernamePasswordAuthorization(this);

So what am I missing?  How can I have it ask for credentials?



        private static void InitializeOAuthConsumerStrings(TwitterContext twitterCtx)
            var oauth = (DesktopOAuthAuthorization)twitterCtx.AuthorizedClient;
            oauth.GetVerifier = () =>
                Console.WriteLine("Next, you'll need to tell Twitter to authorize access.\nThis program will not have access to your credentials, which is the benefit of OAuth.\nOnce you log into Twitter and give this program permission,\n come back to this console.");
                Console.Write("Please enter the PIN that Twitter gives you after authorizing this client: ");
                return Console.ReadLine();

            if (oauth.CachedCredentialsAvailable)
                Console.WriteLine("Skipping OAuth authorization step because that has already been done.");

        private void Form1_Shown(object sender, EventArgs e)

            if (string.IsNullOrEmpty(ConfigurationManager.AppSettings["twitterConsumerKey"]) || string.IsNullOrEmpty(ConfigurationManager.AppSettings["twitterConsumerSecret"]))
                Console.WriteLine("Skipping OAuth authorization demo because twitterConsumerKey and/or twitterConsumerSecret are not set in your .config file.");
                Console.WriteLine("Using username/password authorization instead.");

                // For username/password authorization demo...
                //auth = new UsernamePasswordAuthorization(Utilities.GetConsoleHWnd());
                auth = new UsernamePasswordAuthorization(this);
                Console.WriteLine("Discovered Twitter OAuth consumer key in .config file.  Using OAuth authorization.");

                // For OAuth authorization demo...
                auth = new DesktopOAuthAuthorization();
                // If you wanted to pass the consumer key and secret in programmatically, you could do so as shown here.
                // Otherwise this information is pulled out of your .config file.
                ////var desktopAuth = (DesktopOAuthAuthorization)auth;
                ////desktopAuth.ConsumerKey = "some key";
                ////desktopAuth.ConsumerSecret = "some secret";
            auth.UseCompression = true;

            // TwitterContext is similar to DataContext (LINQ to SQL) or ObjectContext (LINQ to Entities)

            // For Twitter
            twitterCtx = new TwitterContext(auth, "", "");

            // If we're using OAuth, we need to configure it with the ConsumerKey etc. from the user.
            if (twitterCtx.AuthorizedClient is OAuthAuthorization)

            // Whatever authorization module we selected... sign on now.  
            // See the bottom of the method for sign-off procedures.
            catch (OperationCanceledException)
                Console.WriteLine("Login canceled. Demo exiting.");



Aug 5, 2010 at 11:28 PM

Today, without changing anything, the app now asks me for username/password..   the only things that changed is...  Twitter was down for a while, and during me trying it today...  and I tried this line:

//UsernamePasswordAuthorization up = new UsernamePasswordAuthorization();

Just before the line below..   Twitter was down, so nothing worked...I commented the line above and waited until twitter was up... 


new UsernamePasswordAuthorization(this);


ONce twitter was up, all was well. 

The only thing I can think is maybe I had an instance of IE still running that had the credentials cached?  but as I read the code behind the authentication in linq2Twitter, it should clear the cache...

any ideas?

Sep 11, 2010 at 8:27 PM


Whenever you get an unexplained error that comes and goes, it can be the API that's down.  Twitter now has a status dashboard that you can check: