0

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 = ##########
            }
        };
    }
The Jonas Persson
  • 1,726
  • 1
  • 17
  • 36
  • 2
    Don't catch an exception just to re-throw it. You're clearing out the stack trace and adding no additional value. – Servy May 06 '14 at 19:56
  • @Servy, thanks. I'll keep that in mind- it was a temporary solution, though a bad one. – The Jonas Persson May 06 '14 at 19:58
  • Just fyi I'm having trouble with the linqtotwitter streaming API just in the last day; everything was working fine before with the same binaries. Not sure if there's a problem with the library wrt some twitter update or what. – Dax Fohl May 07 '14 at 04:32

1 Answers1

0

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.

The Jonas Persson
  • 1,726
  • 1
  • 17
  • 36