C# ile Twitter İşlemleri Google'da Ara

Blog > C# ile Twitter İşlemleri
15 Temmuz 2010 – 00:27

Bu yazıda meraktan araştırıp bulduğum daha sonra editleyerek class haline getirdiğim kod parçasını paylaşıcam. Kod parçası twitter apilerini kullanıyor ve yaptığını programla twitter hesabınıza follower eklemeye ve tweetlemeye yarıyor.

Öncelikle tweet göndermeye yarayan classımız;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public void PostTweet(string username, string password, string tweet)
        {
            try
            {
                // kullanıcı adı ve şifre encode ediliyor
                string user = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(username + ":" + password));
                byte[] bytes = System.Text.Encoding.ASCII.GetBytes("status=" + tweet);
                // güncelleme sayfasına bağlanılıyor
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://twitter.com/statuses/update.xml");
                // metod post olarak ayarlanıyor.
                request.Method = "POST";
                request.ServicePoint.Expect100Continue = false; // thanks to argodev for this recent change!
                request.Headers.Add("Authorization", "Basic " + user);
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = bytes.Length;
                Stream reqStream = request.GetRequestStream();
                // yazılıyor
                reqStream.Write(bytes, 0, bytes.Length);
                // kapatılıyor
                reqStream.Close();
            }
            catch (Exception ex) {/* burası boş */}
        }

yukarıdaki classı kullanarak C#’ta yaptığını programla twitter hesabınıza tweet gönderebilirsiniz. Kullanımı gayet basit kullanıcıAdı,Şifre,YazılacakOlanTweet şeklinde fonksyionu çağırmamız gerekiyor.

Şimdi sırada birini takip etmek için gerekli olan class var.Öncelikle Friendship adında yapı oluşturuyoruz.

1
2
3
4
5
6
7
public enum FriendshipID
        {
            ByID,
            ByUserID,
            ByScreenName,
            Follow
        }

daha sonra alta fonksiyonumuzu ekliyoruz.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
public string Friendship_Create(string username, string password, FriendshipID friendshipID, string id)
        {
            try
            {
                string responseStr = string.Empty;

                switch (friendshipID)
                {
                    case FriendshipID.ByID:
                        {
                            HttpWebRequest messageRequest = (HttpWebRequest)WebRequest.Create("http://twitter.com/friendships/create/" + id + ".xml");
                            messageRequest.Method = "POST";
                            messageRequest.Credentials = new NetworkCredential(username, password);
                            messageRequest.ContentLength = 0;
                            messageRequest.ContentType = "application/x-www-form-urlencoded";
                            WebResponse response = messageRequest.GetResponse();
                            StreamReader sReader = new StreamReader(response.GetResponseStream());
                            responseStr = sReader.ReadToEnd();

                            break;
                        }
                    case FriendshipID.ByScreenName:
                        {
                            HttpWebRequest messageRequest = (HttpWebRequest)WebRequest.Create("http://twitter.com/friendships/create.xml?screen_name=" + id);
                            messageRequest.Method = "POST";
                            messageRequest.Credentials = new NetworkCredential(username, password);
                            messageRequest.ContentLength = 0;
                            messageRequest.ContentType = "application/x-www-form-urlencoded";
                            WebResponse response = messageRequest.GetResponse();
                            StreamReader sReader = new StreamReader(response.GetResponseStream());
                            responseStr = sReader.ReadToEnd();

                            break;
                        }
                    case FriendshipID.ByUserID:
                        {
                            HttpWebRequest messageRequest = (HttpWebRequest)WebRequest.Create("http://twitter.com/friendships/create.xml?user_id=" + id);
                            messageRequest.Method = "POST";
                            messageRequest.Credentials = new NetworkCredential(username, password);
                            messageRequest.ContentLength = 0;
                            messageRequest.ContentType = "application/x-www-form-urlencoded";
                            WebResponse response = messageRequest.GetResponse();
                            StreamReader sReader = new StreamReader(response.GetResponseStream());
                            responseStr = sReader.ReadToEnd();

                            break;
                        }
                    case FriendshipID.Follow:
                        {
                            HttpWebRequest messageRequest = (HttpWebRequest)WebRequest.Create("http://twitter.com/friendships/create/" + id + ".xml?follow=true");
                            messageRequest.Method = "POST";
                            messageRequest.Credentials = new NetworkCredential(username, password);
                            messageRequest.ContentLength = 0;
                            messageRequest.ContentType = "application/x-www-form-urlencoded";
                            WebResponse response = messageRequest.GetResponse();
                            StreamReader sReader = new StreamReader(response.GetResponseStream());
                            responseStr = sReader.ReadToEnd();

                            break;
                        }
                }
                return responseStr;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

Burada ise takip ediceğiniz kişinin ID mi yoksa Ekran Adı mı gibi kriterlerini yazıyoruz.

1
2
3
4
5
6
//twitterCs.cs classımız
//textBox1.Text kullanıcı adı
//textBox2.Text şifre
//twitterCs.FriendshipID.ByScreenName burada ise ekranda görünen adı ile çağırıcağımızı belirtiyoruz
//textBox3.text burada ise arkadaş olarak eklenicek kişinin adı
t.Friendship_Create(textBox1.Text, textBox2.Text, twitterCs.FriendshipID.ByScreenName, textBox3.text);

Umarım faydalı olmuştur.
Hazırlayan : Mertcan Kurtaran

Etiketler Etiketler: ”,

Benzer Konular:

Yorum Yap

Isim : (gerekli)
E-mail : (gerekli)
Yorumunuz: