Steven
Steven

Reputation: 18859

Linq To Twitter error - "The remote server returned an error: (400) Bad Request"

I'm just trying to simply get the 3 latest tweets for a Twitter account:

List<string> tweets = new List<string>();
var twitterCtx = new TwitterContext();

statusTweets =
    tweet in twitterCtx.Status
    where tweet.Type == StatusType.User &&
        tweet.Count == 3 &&
        tweet.ID == "shanselman"
    select tweet;

foreach (var statusTweet in statusTweets)
{
    tweets.Add(statusTweet.Text);
}

But I'm getting this error when I reach the line with the foreach loop:

The remote server returned an error: (400) Bad Request.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.WebException: The remote server returned an error: (400) Bad Request.

Anyone know what I'm doing wrong?

Upvotes: 1

Views: 1865

Answers (1)

Joe Mayo
Joe Mayo

Reputation: 7513

LINQ to Twitter supports Twitter API v1.1, which requires OAuth authentication on every request. I understand that the error message, 400 Bad Request, is a little confusing because a 401 Unauthorized lets you know that you have an authorization problem. Maybe the 400 is because if you don't try to authenticate at all, the request isn't built properly. Anyway, I'm sure that your problem is that you need to go through the authorization process with OAuth. I wrote some OAuth documentation on the LINQ to Twitter Securing your Application page.

The downloadable source code has examples and there's a sample page on the site.

Once you start using OAuth, you might run into some 401 Unauthorized errors and will want to review the LINQ to Twitter FAQ.

Joe

Upvotes: 1

Related Questions