using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Diagnostics;
using System.Net.Http;
using System.IO;
using System.Threading.Tasks;
using Windows.Security.Cryptography;
using Windows.Storage.Streams;
using Windows.Security.Cryptography.Core;
namespace VS2012_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 async Task<string> Tweet(string text, int count) {
HttpClient hc = new HttpClient();
// ソートされるリスト
SortedDictionary<string, string> sl = new SortedDictionary<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", 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 += Uri.EscapeDataString(_tweet_api) + "&";
// Oauth + データ
work2 += 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(Uri.EscapeDataString(oauth_signature));
// OAuth tool チェック用
Debug.WriteLine(work);
string result = null;
try {
// フォーマットは、 OAuth tool で確認。
hc.DefaultRequestHeaders.Add("Authorization", "OAuth " + work);
// 投稿
result = await hc.GetStringAsync(new Uri(
_tweet_api +
"?" +
"q=" + sl["q"] +
"&count=" + sl["count"]
));
}
catch (Exception ex) {
result = "{\"error\", \"" + ex.Message + "\" }";
}
return result;
}
// Framework 4.5 では必要無い
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 signingKey = _consumer_secret + "&";
signingKey += _secret;
IBuffer keyMaterial = CryptographicBuffer.ConvertStringToBinary(signingKey, BinaryStringEncoding.Utf8);
MacAlgorithmProvider hmacSha1Provider = MacAlgorithmProvider.OpenAlgorithm("HMAC_SHA1");
CryptographicKey macKey = hmacSha1Provider.CreateKey(keyMaterial);
IBuffer dataToBeSigned = CryptographicBuffer.ConvertStringToBinary(target, BinaryStringEncoding.Utf8);
IBuffer signatureBuffer = CryptographicEngine.Sign(macKey, dataToBeSigned);
String signature = CryptographicBuffer.EncodeToBase64String(signatureBuffer);
return signature;
}
}
}