示例#1
0
        public void TestService()
        {
            Token key = new Token
            {
                APIKey      = "D1ymPWHCxpkPOeNIrXZmNjXxZ",
                APISecret   = "",
                BearerToken = "AAAAAAAAAAAAAAAAAAAAAOluKQEAAAAAIzRc6a4zvMqFqqpMx5%2FUH3hYhv0%3DkRzPRlz6D8YkzJcsyg1ld2vsOqhMJMBKo2zOumc7Edf6gNaLFY"
            };
            TwitterSampleService twitterService = new TwitterSampleService(key.APIKey, key.APISecret, key.BearerToken);

            twitterService.StartStream("https://api.twitter.com/2/tweets/sample/stream?expansions=attachments.poll_ids,attachments.media_keys,author_id,entities.mentions.username,geo.place_id,in_reply_to_user_id,referenced_tweets.id,referenced_tweets.id.author_id&user.fields=created_at&media.fields=duration_ms,height,media_key,preview_image_url,type,url,width,public_metrics&place.fields=contained_within,country,country_code,full_name,geo,id,name,place_type&poll.fields=duration_minutes,end_datetime,id,options,voting_status&tweet.fields=attachments,author_id,context_annotations,conversation_id,created_at,entities,geo,id,in_reply_to_user_id,lang,public_metrics,possibly_sensitive,referenced_tweets,reply_settings,source,text,withheld", 2000);
        }
示例#2
0
        /// <summary>
        /// used to get tweets from the sample twitter stream, excute on a seperate thread once the start button is clicked
        /// </summary>
        /// <returns></returns>
        private async Task <int> StartStream()
        {
            //get settings from app config
            string APIKey      = Properties.Settings.Default.APIKey;
            string APISecret   = Properties.Settings.Default.APISecret;
            string BearerToken = Properties.Settings.Default.BearerToken;
            string address     = Properties.Settings.Default.Address;
            int    timeOutMs   = Properties.Settings.Default.TimeOutMS;

            int result = 0;

            //try to start stream via the service
            try
            {
                await Task.Run(() =>
                {
                    while (!_isStop)
                    {
                        TwitterSampleService twitterSampleService = new TwitterSampleService(APIKey, APISecret, BearerToken);
                        twitterSampleService.IncomingDataEvent   += TwitterSampleService_IncomingDataEvent;
                        twitterSampleService.StartStream(address, timeOutMs);
                        _timer.Stop();
                        Thread.Sleep(30000);
                        _timer.Start();
                    }
                    ;
                });

                return(result);
            }
            catch (Exception ex)
            {
                result = 1;
                MessageBox.Show(ex.Message);
                btnStart.Enabled = true;
                return(result);
            }
        }