Search code examples
c#.nettwitterasync-awaitlinq-to-twitter

LinqToTwitter status update only works sometimes


I'm trying to send a tweet using LinqToTwitter. My problem is that it almost never works. I've gotten it to work maybe three times out of thirty, and usually that's when I try to debug. I'm thinking it might have something to do with timing and authentication, but I don't know how to do it. The code runs without errors, but no tweet is generated.

public static async Task SendTweetAsync(string text)
    {
        // Get the authorization credentials
        var auth = GetCredentials();

        // Create the twitter context
        var ctx = new TwitterContext(auth);

        try
        {
            Status responseTweet = await ctx.TweetAsync(text);
        }
        catch (Exception e) {
            throw e;
        }
    }

    private static AspNetAuthorizer GetCredentials()
    {
        return new AspNetAuthorizer
        {
            CredentialStore = new InMemoryCredentialStore()
            {
                ConsumerSecret = "##########",
                ConsumerKey = "##########",
                OAuthToken = "##########",
                OAuthTokenSecret = "##########",
                UserID = ##########
            }
        };
    }

Solution

  • I don't know what happened to the answer that was here before, by @JoeMayo, but the thing that worked for me was to change the AspNetAuthorizer to SingleUserAuthorizer.

    private static SingleUserAuthorizer GetCredentials()
    {
        return new SingleUserAuthorizer
        {
            CredentialStore = new InMemoryCredentialStore()
            {
                ConsumerSecret = "##########",
                ConsumerKey = "##########",
                OAuthToken = "##########",
                OAuthTokenSecret = "##########",
                UserID = ##########
            }
        };
    }
    

    Also, I added await auth.AuthorizeAsync();

    var auth = GetCredentials();
    
    await auth.AuthorizeAsync();
    

    As I said, the credit should really go to @JoeMayo, but his answer is gone.