View on GitHub

LinqToTwitter

LINQ Provider for the Twitter API (C# Twitter Library)

LINQ to Twitter FAQ

1. I get a 401 Unauthorized Exception. How to I figure out the problem?

A 401 Unauthorized message means that the Twitter server is unable to authorize you. There are several causes for this problem and the following checklist should help you work through the problems:

2. I get a 400 Bad Request WebException. How to I figure out the problem?

A LINQ to Twitter builds all of the HTTP requests for you, so there’s only a limited number of scenarios where this error occurs. In Twitter API v1.0, rate limiting errors appeared as 400, but LINQ to Twitter now supports the Twitter API v1.1, which returns 429 Too Many Requests for rate limit errors.

The most likely reason for a 400 Bad Request is that you didn’t authenticate your request. Twitter API v1.1, which LINQ to Twitter supports, requires you to authorize each request. That means you need to use OAuth. For information on how to use OAuth with LINQ to Twitter, visit the [[Securing Your Applications]] page. The downloadable source code on this site has examples that use OAuth too.

As with most errors from Twitter, a good way to debug the problem is to use Fiddler to view the HTTP response.

3. I received an Exception with HTTP Status 429. What does that mean?

This means you’ve exceeded the rate limit for an Twitter API request. Twitter API has a feature called Rate Limiting, where it only allows you to send a number of requests within a period of time. You can fix this problem by designing your algorithm so that it won’t exceed the rate limit. The Rate Limit varies for each API, so you’ll need to design your code accordingly. LINQ to Twitter exposes various parts of the Twitter API’s Rate Limiting features, which includes response headers and queries. The response headers you can check are available via TwitterContext instance properties: RateLimitCurrent, RateLimitRemaining, and RateLimitReset. There are also separate rate limiting properties on the TwitterContext entity: MediaRateLimitCurrent, MediaRateLimitRemaining, and MediaRateLimitReset. These header values provide real-time status of rate limits that you can read right after a command or query. In addition to properties that show response headers, you can make a Help/RateLimits query to find rate limits on all or a filtered set of Twitter APIs. See [[Getting Rate Limits]] for more information. You can find more information on how Twitter handles rate limiting in the Twitter API documentation: https://developer.twitter.com/en/docs/basics/rate-limiting.

4. Why are ScreenName and/or UserID properties null in the User entity response from Twitter?

The ScreenName and UserID properties are input only, allowing you to see the parameters you provided in your query.

A bit of background: Anything used as an input parameter is also looked at in the query response, so if a user omits the parameter in a query but the twitter response contains a value, it was being filtered out of the results. To fix this, I adopted a convention where any return parameters also match input parameters would have a ‘Response’ suffix. e.g. ScreenName (input) and ScreenNameResponse (output). To find which values are input, the docs for each API call contain the input/filter parameters.

5. How do I page through Status and Search timelines?

Please see the blog post, Working with Timelines with LINQ to Twitter, that explains how to do this.

6. I’m not using async/await and my LINQ to Twitter code isn’t working.

You must use async/await with LINQ to Twitter v3.0 or later. You might see deadlocks, race conditions, or undetected unhandled exceptions if you try to use LINQ to Twitter like previous versions. LINQ to Twitter v3.0 is asynchronous. That means you need async methods that use await on queries and commands. You also need to use exception handling (aka try/catch), especially for queries that throw exceptions for not found conditions. The downloadable source code has a set of projects with the _Linq2TwitterDemos__ prefix for several technologies to demonstrate how to use async and await with LINQ to Twitter.

7. I’m receiving an error on assemblies named System.Net.Http... when trying to build.

LINQ to Twitter v3.0 has dependencies on HttpClient, which includes System.Net.Http, System.Net.Http.Extensions, and System.Net.Http.Primitives. You can add references to these assemblies via NuGet:

  1. Right-click on References and select Manage NuGet Packages.
  2. Search for HttpClient.
  3. Install the Microsoft HttpClient package.

Another reason for a related error, “System.IO.FileNotFoundException: Could not load file or assembly System.Net.Http.Primitives”, occurs when a different version is in the GAC. You can solve that issue by adding a binding redirection to your *.config file, like this:

    <dependentAssembly>
        <assemblyIdentity name="System.Net.Http.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.2.22.0" newVersion="4.2.22.0" />
    </dependentAssembly>

If you add LINQ to Twitter from NuGet, these dependencies should be added automatically. For more information on NuGet and versioning, David Ebbo has an excellent set of blog posts:

8. How do I let a user authorize my app one time, without having to re-authorize every time.

The CredentialStore property of the IAuthorizer has the user’s tokens, after authorization. So, after the initial authorization, read the tokens from the CredentialStore and save them in the database. Then check to see if you have tokens for the user the next time you authorize. If you have tokens, populate the authorizer. When the authorizer has all 4 tokens, LINQ to Twitter will allow you to perform queries without authorizing the user again.

The C# console demos code contain the following:

            await auth.AuthorizeAsync();

            // This is how you access credentials after authorization.
            // The oauthToken and oauthTokenSecret do not expire.
            // You can use the userID to associate the credentials with the user.
            // You can save credentials any way you want - database, isolated storage, etc. - it's up to you.
            // You can retrieve and load all 4 credentials on subsequent queries to avoid the need to re-authorize.
            // When you've loaded all 4 credentials, LINQ to Twitter will let you make queries without re-authorizing.
            //
            //var credentials = auth.CredentialStore;
            //string oauthToken = credentials.OAuthToken;
            //string oauthTokenSecret = credentials.OAuthTokenSecret;
            //string screenName = credentials.ScreenName;
            //ulong userID = credentials.UserID;
            //

9. I’m writing an ASP.NET application and my credentials keep going away.

IIS recycles occasionally, which also means that any in-memory state is lost. This includes Session state if you’re using InProc, which is the default. To fix this, reconfigure your application to use State Server or SQL Server. You can do a Bing search on how to configure ASP.NET Session State.

10. Can you write a sample for X?

You can download the source code and browse the documentation on this site to find samples on how to use each API. There are an untold number of scenarios that aren’t covered, but I don’t have the personal time to cover them all or keep up with requests. When you ask, I probably won’t have the time to work up a sample and handle follow-up questions. By that time, you probably will have either figured out the problem or moved on to something else. While I would like to help, some things aren’t practical, so I’ll just have to wish you good luck.

11. I’m new to programming. Can you help me?

I’ve written LINQ to Twitter to help developers when writing applications that work with the Twitter API. Hopefully, the software makes their jobs easier. However, attempting to make a task easy doesn’t imply it’s appropriate for people with no experience. You must know: (1) how to code in C#, F#, or VB, (2) how LINQ works, (3) how to write async code, and (4) have some familiarity with the technology you’re using (e.g. ASP.NET, WPF, Mobile, etc.). Without these things, you’ll struggle. Of course, struggling is how we learn and make steps along the way to continually improve. This is what I do every day, continuously improving my own skills. While I would like to help, I don’t have the time. However, I do know you will be able to figure it out if you stay with the journey.

12. How do I log into the Twitter API with Username and Password?

The Twitter API deprecated Username/Password authentication years ago. These days, you must use OAuth. You can visit [[Securing Your Applications]] for information on using OAuth with LINQ to Twitter. Additionally, there are several examples in the downloadable source code on how to use different types of authorizers with LINQ to Twitter. This is one of the more difficult tasks you will have when working with the Twitter API, or any other REST API that uses OAuth; so reading the documentation, evaluating alternatives, and reviewing the samples is all time well spent. It also helps if you get a sample application working first to get the hard part out of the way.

With all that said, Twitter API does have a feature named XAuth that is based on Username/Password. However, it requires their permission to use. You will receive errors if you use XAuthAuthorizor and your account does not have Twitter API permission. You can visit https://developer.twitter.com and review their documentation on XAuth and investigate how to contact them if you want to ask for permission.

Note: Along the way, you’re likely to encounter HTTP 401 Unauthorized errors and can get help for that in FAQ #1 above.

13. How do I do X, Y, or Z?

LINQ to Twitter has documentation available. You can find it by clicking the Wiki tab at the top of the page. There are also demos in the source code. It’s usually helpful to get the demos running, including OAuth, to make sure you have your account, authorization, and credentials set up properly.