SkyDrive へ移動![]()
Twitter_Search.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.Diagnostics; using System.Security.Cryptography; namespace VS2010_Twitter { class Twitter_Search { private string _consumer_key; private string _consumer_secret; private string _token; private string _secret; private string _tweet_api = "https://api.twitter.com/1.1/search/tweets.json"; public Twitter_Search( string consumer_key, string consumer_secret, string token, string secret ) { _consumer_key = consumer_key; _consumer_secret = consumer_secret; _token = token; _secret = secret; } public void Tweet(string text, int count, DownloadStringCompletedEventHandler newEvent = null) { WebClient wc = new WebClient(); if (newEvent != null) { wc.DownloadStringCompleted += newEvent; } // ソートされるリスト SortedList<string, string> sl = new SortedList<string, string>(); sl.Add("count", count.ToString()); sl.Add("oauth_consumer_key", _consumer_key); sl.Add("oauth_nonce", Nonce()); sl.Add("oauth_signature_method", "HMAC-SHA1"); sl.Add("oauth_timestamp", TimeStamp()); sl.Add("oauth_token", _token); sl.Add("oauth_version", "1.0"); sl.Add("q", rfc3986(Uri.EscapeDataString(text))); // http ヘッダ用シグネチャ作成 string work = ""; foreach (KeyValuePair<string, string> kvp in sl) { if (work != "") { work += "&"; } work += kvp.Key + "=" + kvp.Value; } string work2 = ""; // メソッド work2 += "GET" + "&"; // API URL work2 += rfc3986(Uri.EscapeDataString(_tweet_api)) + "&"; // Oauth + データ work2 += rfc3986(Uri.EscapeDataString(work)); // OAuth tool チェック用 Debug.WriteLine(work2); string oauth_signature = Signature(work2); // ヘッダ情報を作成 work = ""; foreach (KeyValuePair<string, string> kvp in sl) { // oauth_* のみを使用する if (work != "") { if ((kvp.Key + " ").Substring(0, 6) == "oauth_") { work += ", "; } } if ((kvp.Key + " ").Substring(0, 6) == "oauth_") { work += kvp.Key + "=" + Dd(kvp.Value); } } // シグネチャを追加( ヘッダーはソートの必要は無い ) work += ", oauth_signature=" + Dd(rfc3986(Uri.EscapeDataString(oauth_signature))); // OAuth tool チェック用 Debug.WriteLine(work); // フォーマットは、 OAuth tool で確認。 wc.Headers["Authorization"] = "OAuth " + work; // 投稿 wc.DownloadStringAsync(new Uri( _tweet_api + "?" + "q=" + sl["q"] + "&count=" + sl["count"] )); } private string rfc3986(string base_string) { string result = base_string.Replace("!", "%21"); result = result.Replace("'", "%27"); result = result.Replace("(", "%28"); result = result.Replace(")", "%29"); result = result.Replace("*", "%2A"); return result; } // ダブルクォートで挟む private string Dd(string base_string) { return "\"" + base_string + "\""; } private string Nonce() { Random rand = new Random(); int nonce = rand.Next(1000000000); return nonce.ToString(); } // タイムスタンプ private string TimeStamp() { TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0); return Convert.ToInt64(ts.TotalSeconds).ToString(); } // シグネチャ private string Signature(string target) { string work = _consumer_secret + "&" + _secret; byte[] bin = Encoding.UTF8.GetBytes(target); HMACSHA1 hmacsha1 = new HMACSHA1(); hmacsha1.Key = Encoding.UTF8.GetBytes(work); byte[] hash = hmacsha1.ComputeHash(bin); return Convert.ToBase64String(hash); } } }
|
【VS(C#)の最新記事】
- Replit : cs-list
- C# : Excel の新しいブックのデフォルトのシートのセルに直接値をセットして、オートフィルを Range オブジェクトから実行する
- C#( Form ) : ウインドウ枠の無い吹き出しの作成
- C# のタプル( Visual Studio 2017 でテスト )
- C# : インターネット上の JSON ファイルのフォーマットを クラスとして定義して1行でオブジェクト化して使用する
- C# の文法的文字列処理
- C# : System.Data.Odbc によるデータベースのテーブルからのデータ取得処理( サンプルの SQL は MySQL 用です )
- C# : Excel を データベースとして DataGridView に読み込む
- C# : dynamic 型 による Excel へのアクセス
- C# : フォームを表示せずに、通知領域にアイコンを表示させる常駐プログラム
- Microsoft Access に対してSQLを入力してその結果を DataGridView に表示する最も簡単なコード
- C# : System.Data.Odbc データ取得(SELECT)処理( MySQL ) : ※ using 無し( Dispose 実行 )
- C# : SQL 文を外部テキストにして、String.Format でデータ部分を置き換えて利用する
- C# コンソールアプリを AN HTTPD で実行
- C# : SQLServer( SQLExpress ) の SMO を使用してテーブルの CREATE TABLE 文 を取得する
- C# : DataGridView に TKMP.DLL の IMAP(POP3) で受信したメールを非同期に表示する( 添付ファイルも取得 )
- C# : TKMP.DLLを使った、メール送信テンプレート
- C# と VB.net : TKMP.DLL を使って IMAP でメール本文の一覧を取得する( コンソール )
- C# でDataTable と DataSource を使用して、DataGridView にデータを表示するテンプレート( 行をダブルクリックしてダイアログを表示して行データを処理 )
- (C#) / VS2010 または VS2012 : TKMP.DLL(3.1.2 または 3.1.8)を使った、『さくらインターネット』用メール送信テンプレート