3

So I am very new to C#, I started learning it a few days ago and I would like to know how you can tweet with C#. I have searched google a lot, looked at some YouTube videos, but they are all old. I found Twitterizer.net, which has this code:

OAuthTokens tokens = new OAuthTokens();
tokens.AccessToken = "XXX";
tokens.AccessTokenSecret = "XXX";
tokens.ConsumerKey = "XXX";
tokens.ConsumerSecret = "XXX";

TwitterResponse<TwitterStatus> tweetResponse = TwitterStatus.Update(tokens, "Hello,         #Twitterizer");
if (tweetResponse.Result == RequestResult.Success)
{
    messagebox.Show("Message Tweeted!");
}
else
{
   messageBox.Show("cannot tweet");
}

I put this code in a button, 'button1', but it doesn't seem to work it pops up a messagebox saying cannot tweet. I have no idea why this is happening. I put this at the top using twitterizer;. I also got my consumer key, consumersecret, token and tokensecret. So I don't know what is the problem, any help would be greatly appreciated. Thank You!

  • 2
    I wrote a blog post on this subject one time - http://blog.kitchenpc.com/2011/01/22/rise-of-the-twitterbot/ – Mike Christensen Jan 30 '12 at 19:08
  • 1
    out of curiosity what was the value tweetResponse.Result. That might help uncovering the problem. e.g. RateLimited vs. BadRequest – Conrad Frix Jan 30 '12 at 19:09
  • Have you tried stepping through the code. (+1 @ConradFrix) – M3NTA7 Jan 30 '12 at 19:20
  • I'd like to see the error details and the properties in the tweetResponse. (FYI, I'm the author of Twitterizer.) – Ricky Smith Jan 30 '12 at 21:08
  • @RickySmith, maybe it is the references I am using. I downloaded twitterizer2.zip, extracted it and now I have a bunch of folders Async ClientProfile Full ... ...etc From which folder do I use the references from?? I just want to do a simple tweet when the button1 is clicked. – Ashley Smith Jan 30 '12 at 21:27
  • You probably want the 'Full' or 'ClientProfile' binaries, depending on your application's build target. – Ricky Smith Jan 31 '12 at 20:03

2 Answers2

2

You should use the Twitter Callback. Set that up in the Developer section of Twitter.

Try the following

using System;
using System.Diagnostics;
using TweetSharp;
using Hammock.Authentication.OAuth;

public static string Token = "XXX"
public static string TokenSecret = "XXX"
public static string ConsumerKey = "XXX"
public static string ConsumerSecret = "XXX"
public static string Callback = "XXX"

private static TwitterClientInfo TwitterClientInfo = new TwitterClientInfo()
{
    ConsumerKey = ConsumerKey,
    ConsumerSecret = ConsumerSecret,
};

private static TwitterService TwitterService = new TwitterService(TwitterClientInfo);

public static bool SetUpTwitter()
{
    var OAuthCredentials = new OAuthCredentials
    {
        Type = OAuthType.RequestToken,
        SignatureMethod = OAuthSignatureMethod.HmacSha1,
        ParameterHandling = OAuthParameterHandling.HttpAuthorizationHeader,
        ConsumerKey = ConsumerKey,
        ConsumerSecret = ConsumerSecret,
        CallbackUrl = "",
    };

    OAuthRequestToken requestToken = TwitterService.GetRequestToken(Callback);
    Uri authUrl = TwitterService.GetAuthorizationUri(requestToken, Callback);

    Process.Start(authUrl.AbsoluteUri);

    DateTime currentTime = DateTime.Now;
    DateTime endTime = currentTime.AddSeconds(30);
    while (currentTime < endTime)
    {
        currentTime = DateTime.Now;
    }

    OAuthAccessToken accessToken = TwitterService.GetAccessToken(requestToken);
    return SendMessage(accessToken.Token, accessToken.TokenSecret, "Send Sample Tweet");
}

public static bool SendMessage(string token, string tokenSecret, string message)
{
    Token = token;
    TokenSecret = tokenSecret;

    if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(tokenSecret))
        return SetUpTwitter();

    try
    {
        TwitterService.AuthenticateWith(token, tokenSecret);
        TwitterService.SendTweet(message);

        return true;
    }

    catch
    {
        return false;
    }
}
Community
  • 1
  • 1
MyKuLLSKI
  • 5,285
  • 3
  • 20
  • 39
  • Hi thanks for your help, I really appreciate it. I got 11 errors and I have no idea how to fix them: Error 1 'twitter.Form1.TwitterClientInfo' is a 'field' but is used like a 'type' C:\Users\ashley\documents\visual studio 2010\Projects\twitter\twitter\Form1.cs 226 16 twitter – Ashley Smith Jan 30 '12 at 20:28
  • Do I need any dlls for this maybe that is the reason?? Should I add reference to twitterizer.dll and Newtonsoft.Json. – Ashley Smith Jan 30 '12 at 20:41
  • Reference Hammock.ClientProfile, TweetSharp, and Newtonsoft.Json. They all come with TweetSharp – MyKuLLSKI Jan 30 '12 at 20:44
  • Alright I have added those refrences. Now I am getting 5 errors: Error 1 The type or namespace name 'OAuthCredentials' could not be found (are you missing a using directive or an assembly reference?) C:\Users\ashley\documents\visual studio 2010\Projects\ twitter\ twitter\Form1.cs 240 32 twitter Error 2 The name 'OAuthType' does not exist in the current context C:\Users\ashley\documents\visual studio 2010\Projects\ twitter\ twitter\Form1.cs 242 16 twitter – Ashley Smith Jan 30 '12 at 20:50
  • Error 3 The name 'OAuthSignatureMethod' does not exist in the current context C:\Users\ashley\documents\visual studio 2010\Projects\ twitter\ twitter\Form1.cs 243 27 twitter Error 4 The name 'OAuthParameterHandling' does not exist in the current context C:\Users\ashley\documents\visual studio 2010\Projects\ twitter\ twitter\Form1.cs 244 29 twitter Error 5 The name 'Process' does not exist in the current context C:\Users\ashley\documents\visual studio 2010\Projects\ twitter\ twitter\Form1.cs 253 5 twitter – Ashley Smith Jan 30 '12 at 20:52
  • And what are the errors? Make sure everything you get an error you right click on the red squiggly and go to Add Reference and add it – MyKuLLSKI Jan 30 '12 at 20:52
  • Sorry about that, I have added the 5 errors above, thanks for your help. – Ashley Smith Jan 30 '12 at 20:56
  • Alright, I have marked it - Can you give me any suggestions on how to fix those errors at the top? – Ashley Smith Jan 30 '12 at 21:05
  • Never mind all the errors are gone after I put this: using System; using System.Diagnostics; using TweetSharp; using Hammock.Authentication.OAuth; now how would I make it tweet. Whenever, I click button1? Thanks a lot for your great help. – Ashley Smith Jan 30 '12 at 21:39
  • In the Button Click event handler do this: ClassNameOfMyPastedCode.SetUpTwitter(); – MyKuLLSKI Jan 30 '12 at 21:40
  • Thanks to you. In button1, I put this code: SetUpTwitter(); it tweeted once, however, when you click it, it opens up the twitter Authorization app page and that's all it does. I don't know how it tweeted once? I have no idea how to do the callback and stuff. – Ashley Smith Jan 30 '12 at 21:51
  • Eh...I'm sorry but your going to have to learn OO programming and C# before doing this. You have to know the fundamentals first – MyKuLLSKI Jan 30 '12 at 21:53
  • Alright, thank your for all the help and sorry to bother you so much. – Ashley Smith Jan 30 '12 at 21:54
0

Without more information, assisting you isn't possible, so, instead of giving you an exact answer I'll try to help you find out what the underlying issue is.

Place a breakpoint on the TwitterResponse<TwitterStatus> tweetResponse = TwitterStatus.Update(tokens, "Hello, #Twitterizer"); line and debug your application. Step over that line, then inspect the value of tweetResponse.ErrorMessage. This should provide you with the raw error message from Twitter, if there is one. If there isn't, look at tweetResponse.Content. This property will contain the raw response from Twitter, which may be json, xml, or even html, depending on the issue.

If you submit issues pertaining to Twitterizer, these values will almost always be needed to troubleshoot them.

Ricky Smith
  • 2,379
  • 1
  • 13
  • 29