Ejemplo n.º 1
0
        //===================================================================================================
        //最近の自身のホームタイムラインを取得するメソッド
        //返り値:取得したツイートのリスト
        public List<Tweet> GetHomeTimeline()
        {
            string oauthNonce = GenNonce ();
            string timeStamp = GenTimestamp ();

            //署名作成=============================================================================================
            //パラメータ==================
            SortedDictionary<string, string> parameters = new SortedDictionary<string, string> ();
            parameters.Add ("oauth_consumer_key", ConsumerKey);
            parameters.Add ("oauth_signature_method", "HMAC-SHA1");
            parameters.Add ("oauth_timestamp", timeStamp);
            parameters.Add ("oauth_nonce", oauthNonce);
            parameters.Add ("oauth_version", "1.0");
            parameters.Add ("oauth_token", AccessToken);
            //==========================

            string signature = GenSignature ("GET", "https://api.twitter.com/1.1/statuses/home_timeline.json", parameters, ConsumerSecret, AccessTokenSecret);

            //===================================================================================================

            string authHeader = GenAuthHeader (ConsumerKey, oauthNonce, signature, timeStamp, AccessToken);
            //			//ヘッダ作成===========================================================================================
            //			string authHeader = string.Format (
            //				                    "OAuth oauth_consumer_key=\"{0}\", " +
            //				                    "oauth_nonce=\"{1}\", " +
            //				                    "oauth_signature=\"{2}\", " +
            //				                    "oauth_signature_method=\"{3}\", " +
            //				                    "oauth_timestamp=\"{4}\", " +
            //				                    "oauth_token=\"{5}\", " +
            //				                    "oauth_version=\"{6}\""
            //				//APIKeyなども形式的に念のため全てURLエンコードする
            //				, Uri.EscapeDataString (ConsumerKey)
            //				, Uri.EscapeDataString (oauthNonce)
            //				, Uri.EscapeDataString (signature)
            //				, Uri.EscapeDataString ("HMAC-SHA1")
            //				, Uri.EscapeDataString (timeStamp)
            //				, Uri.EscapeDataString (AccessToken)
            //				, Uri.EscapeDataString ("1.0"));
            //			//===================================================================================================

            //get送信=======================================================
            string reqUrl = "https://api.twitter.com/1.1/statuses/home_timeline.json";
            ServicePointManager.Expect100Continue = false;
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create (reqUrl) as HttpWebRequest;
            req.Method = "GET";
            req.ContentType = "application/x-www-form-urlencoded";
            req.Host = "api.twitter.com";
            req.Headers.Add ("Authorization", authHeader);

            HttpWebResponse res = (HttpWebResponse)req.GetResponse ();
            Stream resStream = res.GetResponseStream ();
            StreamReader sr = new StreamReader (resStream);
            //JSONデータを取得
            string resultJson = sr.ReadToEnd ();
            resStream.Close ();
            sr.Close ();
            //JSONデータのパース
            var root = JsonConvert.DeserializeObject<List<RootObject>> (resultJson);
            List<Tweet> resultList = new List<Tweet> ();
            foreach (RootObject r in root) {

                //debug
                Console.WriteLine ("");
                Console.WriteLine (r.id);
                Console.WriteLine (r.text);
                Console.WriteLine (r.user.screen_name);
                Console.WriteLine (r.user.name);
                Console.WriteLine ("");

                User usr = new User (r.user.name, r.user.screen_name);
                Tweet tweet = new Tweet ((long)r.id, r.text, usr);
                resultList.Add (tweet);
            }
            return resultList;
            //=============================================================
        }
Ejemplo n.º 2
0
 //コンストラクタ
 //id:ツイートID
 //text:ツイート内容
 //user:ツイートユーザ
 public Tweet(long id, string text, User user)
 {
     Id = id;
     Text = text;
     User = user;
 }