How to tweet in C#

1.9k views Asked by At

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

There are 2 answers

13
MyKuLLSKI On BEST ANSWER

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;
    }
}
0
Ricky Smith On

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.