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!
You should use the Twitter Callback. Set that up in the Developer section of Twitter.
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;
}
}