开发者

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

开发者 https://www.devze.com 2023-02-05 05:02 出处:网络
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 c

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/


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消