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:
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: ");
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, "https://api.twitter.com/1/", "https://search.twitter.com/");
// 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.
Console.WriteLine("Login canceled. Demo exiting.");