SQLの窓

2013年10月12日


VS2010(C#)WPF : ListView Twitter 検索テンプレート

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#)の最新記事】
posted by lightbox at 2013-10-12 00:20 | VS(C#) | このブログの読者になる | 更新情報をチェックする
container 終わり



フリーフォントで簡単ロゴ作成
フリーフォントでボタン素材作成
フリーフォントで吹き出し画像作成
フリーフォントではんこ画像作成
ほぼ自由に利用できるフリーフォント
フリーフォントの書体見本とサンプル
画像を大きく見る為のウインドウを開くボタンの作成

CSS ドロップシャドウの参考デモ
イラストAC
ぱくたそ
写真素材 足成
フリーフォント一覧
utf8 文字ツール
右サイド 終わり
base 終わり