Hello World to Twitter from C#
A few days ago, I posted this question and this question asking about how to pos开发者_StackOverflow社区t "Hello World" to twitter. I've gotten helpful responses, which have propelled me further along, but I'm still lost.
I need to use OAuth because (as I read it) using username and password is going to be deprecated soon.
I need an example as simple as updating the status with the string constant 'Hello World!'.
My client is specifying that I must use C#.
Definitely use Linq2Twitter -
http://linqtotwitter.codeplex.com/
It's UpdateStatus method has 11 overloads - the whole implementation is really nice. So you're example would be:
var tweet = twitterCtx.UpdateStatus("Hello world");
I'd like to post this here as it took me far too long to work out and is what I would consider to be a minimum requirement for a Hello World to Twitter using Linq2Twitter now that OAuth is mandatory. Hopefully this will be of use to anyone like me who ended up on this page but found it didn't solve their problem.
using LinqToTwitter;
var auth = new SingleUserAuthorizer
{
Credentials = new InMemoryCredentials
{
ConsumerKey = "yourConsumerKey",
ConsumerSecret = "yourConsumerSecret",
OAuthToken = "yourOAuthToken",
AccessToken = "yourAccessToken"
}
};
var service = new TwitterContext(auth);
var tweet = service.UpdateStatus("hello twitter");
What API are you using? have you tried Twitterizer. It should be relatively simple to do.
I highly recommend that you use TweetSharp. It is very robust, supports the scenario you specify above (uses OAuth to authenticate).
I've used it on a few pet projects and I've been extremely happy with it. The download comes with a WPF sample application that shows you how to use twitter's OAuth implementation.
I don't have enough reputation to comment to zithrey but I also agree, linq2twitter's getting started documentation hurts and the project loaded with errors making it unrunnable. Hopefully this will help someone - it uses PIN authorization
static void Main(string[] args)
{
string ckey = "consumerkey";
string csecret = "consumersecret";
var auth = new PinAuthorizer()
{
Credentials = new InMemoryCredentials
{
ConsumerKey = ckey,
ConsumerSecret = csecret
},
GoToTwitterAuthorization = pageLink => Process.Start(pageLink),
GetPin = () =>
{
Console.WriteLine(
"\nAfter authorizing this application, Twitter " +
"will give you a 7-digit PIN Number.\n"
);
Console.Write("Enter the PIN number here: ");
return Console.ReadLine();
}
};
auth.Authorize();
var twitterCtx = new TwitterContext(auth);
twitterCtx.UpdateStatus("This status has been created from a C# console app!");
}
精彩评论