简体   繁体   中英

How to write a command line C# program that posts to Twitter

I'd like to write a console program in C# that posts a Tweet to Twitter. I've never used the Twitter APIs before and don't know anything about how their authentication works. I found an API library called Twitterizer, but it seems geared towards web applications and wants the user to logon with a web browser. All the API docs on Twitter's website seems geared around this scenario as well.

Is it possible to access the Twitter APIs using a console app with no web browser access? I'm perfectly fine hard coding in the name and password for the Twitter user I want to post under as well. Thanks!

Mike

You'll need to use OAuth for authenticating in twitter.

Then use regular HTTP Request to use the twitter JSON-based API.

Here you can find a good article about OAuth, Twitter and console applications.

Also take a loot at linq2twitter lib. From it's documentation;

The Twitter API is built using Representable State Transfer (REST). Wikipaedia defines REST as "...a style of software architecture for distributed hypermedia systems...", but I'm going to be so bold as to try to simplify what that means. In practice, REST is a Web service protocol built upon Hypertext Transfer Protocol (HTTP). You use the REST Web service by making an HTTP call with a URL and getting text back in some form, which is often XML or JSON. So, if you were to write code that made an HTTP request with the following URL:

http://api.twitter.com/1/statuses/public_timeline.xml

You would get back an XML document with all of the Twitter statuses from the public timeline, which is a snapshot in time of the last 20 tweets at the time of your request. Go ahead and open your browser, copy and paste the URL above into the address bar, and see what you get back.

I couldn't find any decent information on the web on how to do this, so I decided to write my own blog post with all the details.. Enjoy!

http://blog.kitchenpc.com/2011/01/22/rise-of-the-twitterbot/

Just wrote a Twitter Bot in C#. This is currently posting tweets to @valuetraderteam .

https://gist.github.com/sdesalas/c82b92200816ecc83af1

The API component in the GIST below is less than 500 lines, only dependency is Json.NET , you'll need to download the latest DLL for either x64 or x86 (depending on what platform you are targetting) and include as a reference in your project.

There is an example at the bottom of the page of how you can make a tweet from a console application

Hopefully this is useful to some other people out there.

of course you can use anything to connect to Twitter via RESTful api.
you should use oauth, and set up your application in http://dev.twitter.com , then you should read all articles listed in documents, you must specify your app as Client but not Browser so user input a number to get through authentication.

you can use many libraries so that you can save your time, all are listed in the documents

and be CAREFUL, you should not use Twitter's own api console which is buggy (as i know parameters somtimes can't be parsed), you should use APIgee instead which is powerful and stable.

if you want use basic authentication, you should use api proxy (one famous is twip), if you just need only one single C# apps, you must code by yourself:

  1. you should use given username and password to login twitter, parse cookies passed
  2. use normal oauth to get temporaly access token url.
  3. use cookies got from step 1, emulates form submit to allow your apps, capture PIN code
  4. use pin code to finish oauth.
  5. MOST IMPORTANT, you must store access token in client's machine so next time you can bypass above steps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM