Search code examples
c#linq-to-twitter

Response of "Faulted" when sending a tweet using linqtotwitter


I am trying to send a simple status update (tweet) using linq to twitter. I can connect ok but when I send a tweet I get a return status of "faulted" using the following code.

public async Task Tweet(string text)
{
    var auth = GetCredentials();
    await auth.AuthorizeAsync();

    var ctx = new TwitterContext(auth);

    var tweet = ctx.TweetAsync(text);

    if (tweet != null)
        Console.WriteLine(tweet.Status);
}

My GetCredentials method is as follows:

private static SingleUserAuthorizer GetCredentials()
{
    return new SingleUserAuthorizer
    {
        CredentialStore = new SingleUserInMemoryCredentialStore()
        {
            ConsumerKey = ####,
            ConsumerSecret = ####,
            AccessToken = ####,
            AccessTokenSecret = ####,
        }
    };
}

To confirm I have created an app in twitter. The credentials are correct (or at least copied and pasted from twitter) and I have full permissions to post messages to the account in question.


Solution

  • You should await TweetAsync, like this:

    Status tweet = await ctx.TweetAsync(text);
    

    That still isn't going to fix your problem all the way and here's why:

    1. Since you weren't awaiting the async operation, you returned Task.
    2. You didn't know this because tweet is var. I will update my demo code to use Status so people will see a compiler error if they use my example.
    3. When you looked at tweet, you were viewing Task.Status, which was TaskStatus.Faulted.
    4. TaskStatus.Faulted occurs because of an unhandled exception.
    5. It's very likely that you received an unhandled exception because of an authentication error - 401 Unauthorized.
    6. I wrote a FAQ with an extensive list of reasons why 401 exceptions occur and items to check for the reason(s): LINQ to Twitter FAQ.
    7. You should wrap the code in a try/catch block, looking for a TwitterQueryException that will have more details, including the error text returned from the Twitter API.