2010-05-17 30 views

回答

15

只要使用該包裝來實現對Twitter的API:

https://github.com/danielcrenna/tweetsharp

var twitter = FluentTwitter.CreateRequest() 
    .AuthenticateAs("USERNAME", "PASSWORD") 
    .Statuses().Update("Hello World!") 
    .AsJson(); 

    var response = twitter.Request(); 

來源:http://code-inside.de/blog-in/2009/04/23/howto-tweet-with-c/

There is even a DotNetRocks interview with the developers.

+1

阿喀琉斯,謝謝你的回答! – 2010-05-21 13:40:14

+2

Broken Link http://tweetsharp.codeplex.com/ – jth41 2014-03-23 03:24:54

+1

看起來現在住在這裏:https://github.com/Yortw/tweetmoasharp – 2016-08-26 02:09:33

1

Twitter也有其自身的API

+0

鏈接已損壞。 – CarneyCode 2017-11-12 08:41:07

5

是的,您可以在沒有任何第三方庫的情況下執行此操作。請參閱我的IronPython sample發佈在IronPython Cookbook網站上,該網站會向您顯示如何。即使你在IronPython的,應該讓你開始下面,易反覆移植到C#代碼的主要部分不計劃:

ServicePointManager.Expect100Continue = False 
wc = WebClient(Credentials = NetworkCredential(username, password)) 
wc.Headers.Add('X-Twitter-Client', 'Pweeter') 
form = NameValueCollection() 
form.Add('status', status) 
wc.UploadValues('http://twitter.com/statuses/update.xml', form) 

基本上,這確實一個HTTP POST到http://twitter.com/statuses/update.xml用單名爲status的HTML FORM字段,其中包含由username(和password)標識的帳戶的狀態更新文本。

+5

今天工作正常,但當Twitter關閉下個月的基本身份驗證時,它將停止工作。 – 2010-05-18 20:40:26

+0

+1用於實際理解問題(使用API​​進行處理)。 – 2010-05-19 18:40:08

6

我創建準確說明如何設置內部的Twitter的應用程序,使用的NuGet安裝的API庫,從用戶接受一個訪問令牌,並張貼在該用戶的代表的視頻教程:

視頻: http://www.youtube.com/watch?v=TGEA1sgMMqU

教程:http://www.markhagan.me/Samples/Grant-Access-And-Tweet-As-Twitter-User-ASPNet

如果你不想離開這個頁面:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

using Twitterizer; 

namespace PostFansTwitter 
{ 
    public partial class twconnect : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      var oauth_consumer_key = "gjxG99ZA5jmJoB3FeXWJZA"; 
      var oauth_consumer_secret = "rsAAtEhVRrXUTNcwEecXqPyDHaOR4KjOuMkpb8g"; 

      if (Request["oauth_token"] == null) 
      { 
       OAuthTokenResponse reqToken = OAuthUtility.GetRequestToken(
        oauth_consumer_key, 
        oauth_consumer_secret, 
        Request.Url.AbsoluteUri); 

       Response.Redirect(string.Format("http://twitter.com/oauth/authorize?oauth_token={0}", 
        reqToken.Token)); 
      } 
      else 
      { 
       string requestToken = Request["oauth_token"].ToString(); 
       string pin = Request["oauth_verifier"].ToString(); 

       var tokens = OAuthUtility.GetAccessToken(
        oauth_consumer_key, 
        oauth_consumer_secret, 
        requestToken, 
        pin); 

       OAuthTokens accesstoken = new OAuthTokens() 
       { 
        AccessToken = tokens.Token, 
        AccessTokenSecret = tokens.TokenSecret, 
        ConsumerKey = oauth_consumer_key, 
        ConsumerSecret = oauth_consumer_secret 
       }; 

       TwitterResponse<TwitterStatus> response = TwitterStatus.Update(
        accesstoken, 
        "Testing!! It works (hopefully)."); 

       if (response.Result == RequestResult.Success) 
       { 
        Response.Write("we did it!"); 
       } 
       else 
       { 
        Response.Write("it's all bad."); 
       } 
      } 
     } 
    } 
}