0

一个月前在 c# Windows 窗体应用程序中构建了一个 twitter 工具。我可以发送推文并查看家庭时间线、朋友时间线和提及时间线。我使用 twitterizer dll 和 OAuthTokens 方法,但是有刷新限制,而且速度很慢。

现在我想启动一个流式 Twitter 应用程序,但我找不到合适的示例或文档。我开始了,但我没有找到可以启动流的函数,但它在字符串或文本框中。我的问题是如何从主时间线获取流。

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

1 回答 1

0

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

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

于 2012-11-29T21:48:33.257 回答