Geting Started with LinqToTwitter

Jul 8, 2011 at 11:03 PM
Edited Jul 8, 2011 at 11:06 PM

Hi everybody! I'm getting started with the Twitter API using the LinqToTwitter Library to develop a desktop application (WPF).

I'm using C# and the Framework 3.5, but I'm having problems with the authentication, I made an example using the public tweets and everything went fine but when I wrote my cosumer key, consumer secret, access token and access token secret it returns an exception that says something like:  "401 Unauthorized" and I'm not sure what's the reason, I don't know if is something about my tokens or something about the Configuration file or what. If someone could help me I'll be very grateful. 

Jul 9, 2011 at 6:45 PM

Hi sarahimireles,

The 401 means that you haven't authenticated properly with Twitter, which could be because of a number of issues.  Starting off, it's a good idea to download the code and step through the LinqToTwitterDemo project.  Take a look at app.config - you only need to start with ConsumeKey and ConsumerSecret.  I recently added an  article on how to do this in the L2T docs:

Pay attention to the tips on how to set up your application because these are common gottchas when getting started.

Once you have ConsumeKey and ConsumerSecret set in your app.config, you can run the demo project.  After the application authorizes, which happens after you provide the Pin, the Credentials of the authorizer will contain the OAuthToken and AccessToken that you can use on all subsequent queries.  Write code to grab these values and save them in a DB for that user.  Whenever you make a new query, grab these values from the DB and populate the Credentials, which will prevent a new OAuth dance because you're already authenticated.  Twitter doesn't change the OAuthToken and AccessToken for the user, which is why you can use them again.

If you can get the demo working (without 401s), then you know you're working with the right credentials.


Jul 11, 2011 at 11:30 PM
Edited Jul 11, 2011 at 11:37 PM

Ok, the example "Configuring OAuth" was very helpful, but I have another question and I hope you can help me because I've been searching about it and can't find anything. How can I get the Pin? I look everywhere and I can't find it on the app details that twitter provides. How does it works?

Thanks for the help :)

Jul 12, 2011 at 7:57 PM

Twitter will give you the PIN.  The PinAuthorizer supports an OAuth workflow that works for non-website applications, such as Console, Windows Forms, or WPF.  Here's how it works:

1. When you do an Authorize(), LINQ to Twitter launches a browser to the Twitter authorization page.

2. The user logs in, if necessary, and clicks the button to authorize.

3. Twitter redirects to a new page that has the PIN number.  This is where WebAuthorizer differs because with Web authorization, Twitter will redirect back to the Web page that kicked off the authorization.  However, it's extremely difficult/impossible for Twitter to redirect back to a desktop app.  So, the alternative is for Twitter to redirect to a page with the PIN.

4. The user copies the PIN and returns to the application and gives the PIN to the application.  That's where you'll see the lambda assigned to the GetPin Action property, which reads the PIN.

5. LINQ to Twitter then exchanges the PIN for an AccessToken, and the authorizer is ready to be assigned to the TwitterContext instance.

Once you have a TwitterContext instance with an authorizer that contains all 4 credential elements, you can use that instance to make queries.


Jul 12, 2011 at 10:53 PM

Thank you very much! I finally could run the demo and it works. :)

Jul 13, 2011 at 11:52 PM

Hi Joe! I tried the demo a couple of times and it works but after that, it stoped work and started to say again "The remote server returned an error: (401) Unauthorized. " I downloaded the code again and just tried entering my consumer key and consumer secret and it keeps saying the same error.

 What could be happening?

Jul 14, 2011 at 2:56 AM

I don't know if this will help, but I saw a post, Troubleshooting OAuth 1.0A, on the new Twitter Dev site that suggests checking the system time.  Essentially, LINQ to Twitter is building the OAuth signature based on the time of the local machine.  If the local machine's time differs significantly from the Twitter server time, authentication can fail.  Here's how you could check that:

1. Open Fiddler2.

2. Run the program, keeping an eye on when the Fiddler response comes back.

3. Right-click on the Fiddler response, select properties, and observe the request time.

4. Look at the Date header of the response in the lower right panel, this is the Twitter server time of request.

5. There will be a time difference because Twitter is using GMT (UTC), unless your time zone is GMT.

6.  Compare the times.  I've noticed that there seems to be a fair amount of variance, nearly two minutes in my case, and the request is still good.

7.  If your clock is significantly different from Twitter's server, the request can fail, so you might want to consider adjusting your system time so that it's closer to Twitter's.


Jul 14, 2011 at 11:09 PM

I checked that, the machine had a wrong time zone, I change it and it works now always. Thanks!