← back to articles

❯ cat c-twitter-class.md

C# Twitter Class

2 min readtr#C##Twitter

Ufak bir araştırma neticesinde bulduğum ve değişiklikleri yapmış olduğum class twitter apilerini kullanıyor yazacağımız methodlar ve classımız twitter hesabımıza follower eklemeye ve tweetlemeye yarıyor.

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

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;
                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ş */}
        }

Yazdığımız class ı kullanarak c# da yazacağımız programla twitter hesabımıza tweet gönderebiliyoruz.
Şimdide birini takip etmemiz için Friendship adında yapı oluşturuyoruz.

public enum FriendshipID
        {
            ByID,
            ByUserID,
            ByScreenName,
            Follow
        }

Daha sonrada hemen altına fonksiyonumuzu ekliyoruz.

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.

//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);