Cannot Authenticate streaming api

Jul 4, 2013 at 1:59 PM
Edited Jul 4, 2013 at 2:00 PM
Hi,

i cannot get any authentication to streaming api. Cuz search api returns an errorr like "Rate limit exceeded".
I got a timer based loop. And i could not recieve response from twitter.
Here is my code. Please help me.
        public Form1()
        {
            InitializeComponent();
            auth = new SingleUserAuthorizer
            {
                Credentials = new InMemoryCredentials
                {
                    ConsumerKey = "consumer key",
                    ConsumerSecret = "consumer secret",
                    OAuthToken = "access token",
                    AccessToken = "access token secret"
                }
            };
            twitterCtx = new TwitterContext(auth as SingleUserAuthorizer);
            teamA_count = 0;
            teamB_count = 0;
            aTimer = new System.Timers.Timer(5000);
            aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
            aTimer.Start();
            //bTimer = new System.Timers.Timer(5000);
            //bTimer.Elapsed += new ElapsedEventHandler(bTimer_OnTimedEvent);
            //bTimer.Start();

        }
        private void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            string status;
            var query = (from o in twitterCtx.Streaming where o.Type == StreamingType.Filter && o.Track == "#hashtag" select o);
            query.StreamingCallback(x => status = x.Content);
        }
x.Content always turns null.
Coordinator
Jul 4, 2013 at 3:07 PM
Hi,

The problem is occurring because your code opens and closes the stream too many times. By design, the stream should open one time and stay open. To approach fixing this, consider keeping the query reference in a field that doesn't go out of scope. Then, instead of using a timer, just use the same code that creates the query, but call it only one time. You should also implement error handling inside of the StreamingCallback so that you can re-start the stream if you encounter an exception or Twitter closes the stream. You can see the StreamingDemo in the LinqToTwitterDemos project where I demonstrate how to use the various properties of the StreamingCallback parameter.

Also, it's useful to review Twitter's Streaming Documentation, which will give you better insight into what they intend. There's a lot of info there, but it's valuable for making streaming work well - e.g. error handling, back-off strategies, and more.

@JoeMayo
Jul 4, 2013 at 3:40 PM
Hi,

Thank You for helping.
There was a missing FirstOrDefault. I solved it!
Now it works.
            var query = (from o in twitterCtx.Streaming where o.Type == StreamingType.Filter && o.Track == "#hashtag" select o)
                .StreamingCallback(strm =>
                {
                    if (strm.Status == TwitterErrorStatus.RequestProcessingException)
                    {
                        Console.Write(strm.Error.ToString());
                        error = strm.Error.ToString();
                        return;
                    }
                    status = strm.Content;
                    Console.Write(strm.Content + Environment.NewLine);
                }).FirstOrDefault();
Jul 8, 2013 at 2:47 PM
Hi,

I am experiencing a similar problem. I am calling the sample method from StreamingDemo a bunch of times, but never is the streamingCallBack method called. It just won 't execute the lambda. Problems with authorization maybe? I don't get an error, it just won 't go into the expression.

Thanks!
private static void SamplesDemo(TwitterContext twitterCtx)
        {
            Console.WriteLine("\nStreamed Content: \n");
            int count = 0;

            (from strm in twitterCtx.Streaming
             where strm.Type == StreamingType.Sample
             select strm)
            .StreamingCallback(strm =>
            {
                if (strm.Status == TwitterErrorStatus.RequestProcessingException)
                {
                    Console.WriteLine(strm.Error.ToString());
                    return;
                }

                Console.WriteLine(strm.Content + "\n");

                if (count++ >= 10)
                {
                    strm.CloseStream();
                }
            })
            .SingleOrDefault();
        }