2012-11-29 60 views
0

一個月前在c#Windows Form Application中構建一個推特工具。我可以發送推文並查看家庭遊戲時間線,friendtimeline和提及時間線。我使用了OAuthTokens方法的twitterizer dll,但有刷新限制,速度非常慢。從twitter獲取userstream

現在我想開始一個流twitter應用程序,但我無法找到一個適當的例子或文件。我開始,但我沒有找到可以啓動流的功能,但它在一個字符串或文本框中。我的問題是如何從hometimeline獲取流。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using Twitterizer; 
using Twitterizer.Streaming; 

namespace Twww 
{ 
    public partial class Form1 : Form 
    { 
     public OAuthTokens tokens = new OAuthTokens(); 
     private string userAgent = null; 

     public Form1() 
     { 
      InitializeComponent(); 
      /* input tokens removed in example code */ 

      tokens.AccessToken = accessToken; 

      tokens.AccessTokenSecret = accesssecret; 

      tokens.ConsumerKey = consumerKey; 

      tokens.ConsumerSecret = consumerSecret; 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      Twitterizer.Streaming.UserStreamOptions options = new UserStreamOptions(); 
      OAuthTokens token = new Twitterizer.OAuthTokens(); 
      TwitterStream stream = new TwitterStream(token, userAgent, options); 
     } 
    } 
} 

我試圖使它對用戶流工作,但我得到401或403錯誤,誰說我沒有權利

using Newtonsoft.Json; 
using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Net; 
using System.Text; 
using System.Threading.Tasks; 
using Twitterizer; 

namespace ConsoleApplication2 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      OAuthTokens tokens = new OAuthTokens(); 
      string consumerKey = "XX"; 
      string consumerSecret = "XX"; 
      string accessToken = "XX"; 
      string accesssecret = "XX"; 

      tokens.AccessToken = accessToken; 
      tokens.AccessTokenSecret = accesssecret; 
      tokens.ConsumerKey = consumerKey; 
      tokens.ConsumerSecret = consumerSecret; 

      string url = "https://userstream.twitter.com/1.1/user.json?with=followings&replies=all"; 
      WebClient wc = new WebClient(); 
      wc.Credentials = new NetworkCredential("XX", "XX"); 
      StreamReader reader = new StreamReader(wc.OpenRead(url)); 

      string line; 
      while ((line = reader.ReadLine()) != null) 
      { 
       dynamic json = JsonConvert.DeserializeObject(line); 
       if (json.text != null) 
       { 
        Console.WriteLine(json.text); 
       } 
      } 
     } 
    } 
} 
+0

'我試圖讓它適用於用戶流,.....'。我希望引用您從代碼複製的答案。我會刪除我的。祝你好運。 –

回答