从twitter获取用户梦想

时间:2012-11-29 21:46:00

标签: c#

一个月前,在c #Windows Form Application中构建了一个twitter工具。我可以发送推文并查看hometimeline,friendtimeline和提及时间线。我使用了twitterizer dll和OAuthTokens方法,但是有一个刷新限制并且它非常慢。

现在我想开始一个流媒体推特应用程序,但我无法找到适当的示例或文档。我开始但我没有找到可以启动流的功能,但它在字符串或文本框中。我的问题是如何从家庭时间线获取流。

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);
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

检查一下,你在twitterizer中有一个设计精良的TwitterStream指南:

http://thewayofcode.wordpress.com/tag/json/