Search code examples
c#twittertwitter-oauthtweetsharp

Sending a tweet from a C# Application (unattended)


How can I send a tweet from an unattended C# application? So far, I've tried TweetSharp, but that isn't working for me (no errors, but no success either). Here's the background info... I have set up two twitter accounts to test this (lets call them TwitterSender and TwitterReceiver). TwitterReceiver is "following" TwitterSender. I went to dev.twitter.com and logged in as TwitterSender and clicked the "Create New App" button. From here, I was able to find things called "API Key", "API Secret", "Consumer Key", "Consumer Secret", "Access Token", and "Access Token Secret". When I'm logged in as TwitterSender I can see that I have granted read/write/direct message access. How can I tie all this together so that I can simply run the C# console application and have it send a tweet ("Hello World!") from TwitterSender so that any followers (e.g. TwitterReceiver) will get it? I'm OK with hard-coding user/password in plain-text. In the code below, I get no errors at all, but ultimately TwitterStatus ends up being null, and there is no indication that a tweet was sent either from the perspective of TwitterSender or TwitterReceiver.

TwitterClientInfo twitterClientInfo = new TwitterClientInfo();
twitterClientInfo.ConsumerKey = ConsumerKey; //Read ConsumerKey out of the app.config
twitterClientInfo.ConsumerSecret = ConsumerSecret; //Read the ConsumerSecret out the app.config
TwitterService twitterService = new TwitterService(twitterClientInfo);
twitterService.AuthenticateWith(AccessToken, AccessTokenSecret);
Console.WriteLine("Enter a Tweet");
string tweetMessage;
tweetMessage = Console.ReadLine();
TwitterStatus twitterStatus = twitterService.SendTweet(tweetMessage);

Solution

  • I got this working finally! In short, to send a tweet from, say, a Windows form without requiring a user to log in, you would do the following:

    1. go to dev.twitter.com, log in as the account you want to send tweets from, and then create an "Application". A twitter "Application" in this sense is really just an authorization mechanism and not something that a user interacts with at all.
    2. You will be asked for a website during the process of creating the application...After watching a Turkish video tutorial on how this was done, I entered http://www.google.com/tr, but it's likely that Uzbekistan ("uz" I believe) would work equally well). No, I have no idea why you need this.
    3. Leave "Callback URL" blank and uncheck "Allow this application to sign in with Twitter".
    4. Set the permissions of your application to read/write
    5. Generate your OAuth keys - there should be four keys: the "API Key" (AKA Consumer Key), "API Secret" (AKA ConsumerSecret), "Token Key", and "Token Secret".
    6. Now, go to twitter.com and log into your account (different than dev.twitter.com). Under "Settings" (hidden under the gear icon I think), there should be an "Apps" tab where you should see the application that you just created. You may need to grant it permissions from here (or it may already be set). You can also revoke permissions here.
    7. Using Visual Studio 2012 and .NET framework 4.5 (NOT VS2010), get TweetSharp from NuGet. This project is no longer maintained or supported by the original developer, so don't expect much documentation or guidance if things don't work.
    8. Write your code. For example, in a button click event...

      var service = new TweetSharp.TwitterService("ConsumerKey","ConsumerSecret","TokenKey","TokenSecretKey"); //Replace keys with values from step #5
      var twitterStatus = service.SendTweet(new SendTweetOptions() { Status ="Hello World" });
      if (twitterStatus != null)
      {
      MessageBox.Show("It worked");
      }
      

    Note - You can't send the exact same tweet more than once per ___ (some undocumented amount of time). When there is an authentication problem, twitterStatus will return null - if that happens, check your keys, perhaps repermission and regenerate them again.