【问题标题】:How can I get tokens via TweetSharp and create a service with it?如何通过 TweetSharp 获取令牌并使用它创建服务?
【发布时间】:2026-01-01 22:45:01
【问题描述】:

我可以使用 basic,但如何通过 TweetSharp 获取令牌并使用它创建服务?我必须用 Oauth 重写它。

        public Form1()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
            listBox1.MouseDoubleClick += new MouseEventHandler(listBox1_DoubleClick);
            listBox2.MouseDoubleClick += new MouseEventHandler(listBox2_DoubleClick);

        }

         // Basic Authorize

        private void listBox2_DoubleClick(object sender, MouseEventArgs e)
        {
            listBox2.Items.Remove(listBox2.Text);
        }
        private void listBox1_DoubleClick(object sender, MouseEventArgs e)
        {
            listBox2.Items.Add(listBox1.Text);
            listBox1.Items.Remove(listBox1.Text);
        }

        // Basic Authorize

        TwitterService tweetservis3 = new TwitterService("x", "x", "x", "x");
        TwitterService tweetservis2 = new TwitterService("x", "x", "x", "x");
        TwitterService tweetservis1 = new TwitterService("x", "x", "x", "x");
        TwitterService tweetservis = new TwitterService("x", "x", "x", "x");
        TwitterService tweetservis4 = new TwitterService("x", "x", "x", "x");
        TwitterService tweetservis5 = new TwitterService("x", "x", "x", "x");
        TwitterService tweetservis6 = new TwitterService("x", "x", "x", "x");

        private void button1_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();

            try
            {
                var list = tweetservis.ListTweetsOnHomeTimeline(new          ListTweetsOnHomeTimelineOptions());
                foreach (var item in list)
                    listBox1.Items.Add(item.Text);
                label1.Text = "Api 1 Kullanıldı.";
            }
            catch (Exception)
            {
                try
                {
                    var list = tweetservis1.ListTweetsOnHomeTimeline(new ListTweetsOnHomeTimelineOptions());
                    foreach (var item in list)
                        listBox1.Items.Add(item.Text);
                    label1.Text = "Api 2 Kullanıldı.";
                }
                catch (Exception)
                {

                    try
                    {
                        var list = tweetservis2.ListTweetsOnHomeTimeline(new ListTweetsOnHomeTimelineOptions());
                        foreach (var item in list)
                            listBox1.Items.Add(item.Text);
                        label1.Text = "Api 3 Kullanıldı.";
                    }
                    catch (Exception)
                    {

                        try
                        {
                            var list = tweetservis3.ListTweetsOnHomeTimeline(new ListTweetsOnHomeTimelineOptions());
                            foreach (var item in list)
                                listBox1.Items.Add(item.Text);
                            label1.Text = "Api 4 Kullanıldı.";
                        }
                        catch (Exception)
                        {

                            label1.Text = "Limit Dolu";
                        }
                    }

                }
            }
        }



        private void button2_Click(object sender, EventArgs e)
        {
            {
                listBox1.Items.Clear();

                try
                {
                    var collection = tweetservis.ListTweetsOnUserTimeline(new ListTweetsOnUserTimelineOptions() { ScreenName = textBox1.Text, Count = 200 });
                    foreach (var item in collection)
                        listBox1.Items.Add(item.Text);               
                    label1.Text = "Api 1 Kullanıldı.";
                }
                catch (Exception)
                {
                    try
                    {
                        var collection = tweetservis1.ListTweetsOnUserTimeline(new ListTweetsOnUserTimelineOptions() { ScreenName = textBox1.Text, Count = 200 });
                        foreach (var item in collection)
                            listBox1.Items.Add(item.Text);

                        label1.Text = "Api 2 Kullanıldı.";
                    }
                    catch (Exception)
                    {

                        try
                        {
                            var collection = tweetservis2.ListTweetsOnUserTimeline(new ListTweetsOnUserTimelineOptions() { ScreenName = textBox1.Text, Count = 200 });
                            foreach (var item in collection)
                                listBox1.Items.Add(item.Text);

                            label1.Text = "Api 3 Kullanıldı.";
                        }
                        catch (Exception)
                        {

                            try
                            {
                                var collection = tweetservis3.ListTweetsOnUserTimeline(new ListTweetsOnUserTimelineOptions() { ScreenName = textBox1.Text, Count = 200 });
                                foreach (var item in collection)
                                    listBox1.Items.Add(item.Text);

                                label1.Text = "Api 4 Kullanıldı.";
                            }
                            catch (Exception)
                            {

                                label1.Text = "Limit Dolu";
                            }
                        }

                    }

                }




        }

        }


        private void button4_Click(object sender, EventArgs e)
        {
            listBox2.Items.Add(listBox1.Text);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            listBox2.Items.Add(richTextBox1.Text);
            richTextBox1.Clear();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            progressBar1.Value = 0;
            listBox2.SelectedItem = listBox2.Text;
            progressBar1.Value = 10;
            listBox2.SetSelected(0, true);
            progressBar1.Value = 20;
            int i = listBox2.Items.Count;
            progressBar1.Value = 60;
            var count = label3.Text + "kere tweetlendi.";


             try
        {

            tweetservis.SendTweet(new SendTweetOptions() { Status = listBox2.Text });
            label1.Text = "Çalındı...(Api1 ile )";
            listBox2.Items.Remove(listBox2.Text);



        }
        catch (Exception)
        {
            try
            {

                tweetservis1.SendTweet(new SendTweetOptions() { Status = listBox2.Text });
                label1.Text = "Çalındı...(Api2 ile )";
                listBox2.Items.Remove(listBox2.Text);
            }
            catch (Exception)
            {

                try
                {

                    tweetservis2.SendTweet(new SendTweetOptions() { Status = listBox2.Text });
                    label1.Text = "Çalındı...(Api3 ile )";
                    listBox2.Items.Remove(listBox2.Text);
                }
                catch (Exception)
                {

                    try
                    {

                        tweetservis3.SendTweet(new SendTweetOptions() { Status = listBox2.Text });
                        label1.Text = "Çalındı...(Api4 ile )";
                        listBox2.Items.Remove(listBox2.Text);
                    }
                    catch (Exception)
                    {

                        label1.Text = "Limit Dolu";
                    }
                }


    }

            }

            progressBar1.Value = 100;


        }


        private void button7_Click(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            listBox2.Items.Remove(listBox2.SelectedItem);
        }

        private void button9_Click(object sender, EventArgs e)
        {
            var collection = tweetservis.ListTweetsOnUserTimeline(new ListTweetsOnUserTimelineOptions() { ScreenName = textBox1.Text, Count = 100 });
                    foreach (var item in collection)
                        listBox2.Items.Add(item.Text);
        }

        private void XXX()
        {
                progressBar2.Value = 0;
                progressBar3.Value = 0;
                progressBar1.Maximum = (listBox2.Items.Count * 100) + 100 ;
                tweetservis3.SendTweet(new SendTweetOptions() { Status = listBox2.Text });
                label1.Text = "Tweet Gönderildi";
                progressBar1.Value += 10;
                progressBar2.Value += 10;
                label1.Text = "Tweet Gönderildi";
                progressBar1.Value += 10;
                progressBar2.Value += 10;
                progressBar1.Value += 10;
                progressBar2.Value += 10;
                listBox2.Items.Remove(listBox2.Text);
                progressBar1.Value += 10;
                progressBar2.Value += 10;
                label1.Text = "Sıradaki Hazırlandı!";
                listBox2.SetSelected(0, true);
                progressBar2.Value += 10;
                progressBar1.Value += 10;
                label1.Text = "Sıradaki Seçildi!";
                progressBar1.Value += 10;
                progressBar2.Value += 15;
                backgroundWorker1.ReportProgress(0);
                label1.Text = "Durum İşlece Bildirildi!";
                progressBar1.Value += 10;
                progressBar2.Value += 15;
                int kk = sure.Text.Length * 1000;
                progressBar3.Maximum = kk;
                while (kk > 0)
                {
                    System.Threading.Thread.Sleep(200);
                    try
                    {
                         kk -= 200;
                         progressBar3.Value += 200;
                    }
                    catch (Exception)
                    {

                        throw;
                    }
                }
                progressBar1.Value += 10;
                progressBar2.Value += 10;
                label2.Text = "Süre Boyunca Beklendi";
                progressBar1.Value += 10;
                progressBar2.Value += 10;
                label1.Text = "Tamam";
        }

        void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {

        }

         void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            int i = listBox2.Items.Count;

            label2.Text = "1.Adım Var";
        progressBar1.Maximum = i * 100;
            if (i > 0)
            {
                int iii = 0;
                while (i > 0)
                {

                    XXX();
                    progressBar1.Value += 10;
                    iii += 1;
                    int b = i - iii;
                    label3.Text =  b.ToString();


                }
            }
            else
            {
                MessageBox.Show("Tamamlandı.");
            }
        }
         private void button5_Click(object sender, EventArgs e)            //İptal
        {
            SaveList();

        }

         private void button10_Click(object sender, EventArgs e)
         {
         //    var a = listBox1.Items.Contains("@");
           //   foreach (var item in a)
             //{
               //  listBox2.Items.Add(item);
             //}



         }
         private void SaveList()
         {
             var saveFile = new SaveFileDialog();
             saveFile.Filter = "Text (*.txt)|*.txt";
             if (saveFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
             {
                 using (var sw = new StreamWriter(saveFile.FileName, false))
                     foreach (var item in listBox2.Items)
                         sw.Write(item.ToString() + Environment.NewLine);
                 MessageBox.Show("Success");
             }
         }

         private void OpenList()
         {
             var openfile = new OpenFileDialog();
             openfile.Filter = "Text (*.txt)|*.txt";
             if (openfile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
             {
                string myfile = openfile.FileName;
                string[] allLines = File.ReadAllLines(myfile);

                foreach (string line in allLines)
                {
                    listBox2.Items.Add(line);
                }


             }
         }

         private void button11_Click(object sender, EventArgs e)
         {
             OpenList();
         }
    }
}

二手的;

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 TweetSharp; 
using System.Threading; 
using System.Xml; 
using System.IO;

【问题讨论】:

    标签: c# twitter-oauth tweetsharp


    【解决方案1】:

    这是我对 Facebook 所做的类似事情。希望对您有所帮助:

    public void AccessTokenGet(string authToken)
            {
                this.Token = authToken;
                string accessTokenUrl = string.Format("{0}?client_id={1}&redirect_uri={2}&client_secret={3}&code={4}",
                ACCESS_TOKEN, this.ConsumerKey, CALLBACK_URL, this.ConsumerSecret, authToken);
    
                string response = WebRequest(Method.GET, accessTokenUrl, String.Empty);
    
                if (response.Length > 0)
                {
                    //Store the returned access_token
                    NameValueCollection qs = HttpUtility.ParseQueryString(response);
    
                    if (qs["access_token"] != null)
                    {
                        this.Token = qs["access_token"];
                    }
                }
            }
    

    【讨论】: