From 7c75f0f306dd01436756102470ebf17212af0351 Mon Sep 17 00:00:00 2001 From: Syuilo Date: Fri, 16 May 2014 15:25:11 +0900 Subject: [PATCH] #3 #4 fixed --- HTTP/Twitter/OAuth/OAuthCore.cs | 4 +-- HTTP/TwitterRequest.cs | 4 +-- Twitch.csproj | 1 + Utility/Twitter/Authorize.cs | 56 +++++++++++++++++++------------- Vermillion/Utility/RandomText.cs | 12 +++++++ 5 files changed, 50 insertions(+), 27 deletions(-) create mode 100644 Vermillion/Utility/RandomText.cs diff --git a/HTTP/Twitter/OAuth/OAuthCore.cs b/HTTP/Twitter/OAuth/OAuthCore.cs index 78ca939..548aec5 100644 --- a/HTTP/Twitter/OAuth/OAuthCore.cs +++ b/HTTP/Twitter/OAuth/OAuthCore.cs @@ -99,7 +99,7 @@ public static string GenerateSignature( Console.WriteLine("-\t-\t## シグネチャを生成します"); #endif - SortedDictionary Parameters = new SortedDictionary(); + var Parameters = new SortedDictionary(); Parameters.Add("oauth_consumer_key", Context.ConsumerKey); Parameters.Add("oauth_nonce", Nonce); @@ -188,7 +188,7 @@ public static string UrlEncode(string value, Encoding encode) { string unreservedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~"; - StringBuilder result = new StringBuilder(); + var result = new StringBuilder(); byte[] data = encode.GetBytes(value); int len = data.Length; diff --git a/HTTP/TwitterRequest.cs b/HTTP/TwitterRequest.cs index f1b191b..df91f79 100644 --- a/HTTP/TwitterRequest.cs +++ b/HTTP/TwitterRequest.cs @@ -120,7 +120,7 @@ public async Task Request() } // リクエストの作成 - HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(url); + var Request = (HttpWebRequest)WebRequest.Create(url); if (this.Proxy != null) Request.Proxy = new WebProxy(this.Proxy); @@ -146,7 +146,7 @@ public async Task Request() try { // リクエストの送信 - HttpWebResponse Response = (HttpWebResponse)await Request.GetResponseAsync(); + var Response = (HttpWebResponse)await Request.GetResponseAsync(); using (StreamReader ResponseDataStream = new StreamReader(Response.GetResponseStream())) { diff --git a/Twitch.csproj b/Twitch.csproj index 9439563..e52db23 100644 --- a/Twitch.csproj +++ b/Twitch.csproj @@ -125,6 +125,7 @@ + diff --git a/Utility/Twitter/Authorize.cs b/Utility/Twitter/Authorize.cs index 63d5239..5aa1f5c 100644 --- a/Utility/Twitter/Authorize.cs +++ b/Utility/Twitter/Authorize.cs @@ -116,40 +116,50 @@ public void ShowAuthorizeBrowser() public async Task GetAccessTokenFromScreenNameAndPassword( string ScreenName, string Password) { - System.Net.WebClient wc = new System.Net.WebClient(); - string sorce = wc.DownloadString("https://twitter.com"); - wc.Dispose(); + if (this.OAuthToken == null) + throw new NullReferenceException("リクエスト トークンが設定されていません。"); + + try + { + var wc = new System.Net.WebClient(); + string sorce = wc.DownloadString("https://twitter.com"); + wc.Dispose(); - Regex reg = new Regex(".*?)\">", - RegexOptions.IgnoreCase | RegexOptions.Singleline); + Regex reg = new Regex(".*?)\">", + RegexOptions.IgnoreCase | RegexOptions.Singleline); - Match m = reg.Match(sorce); - Console.WriteLine("authenticity_token: " + m.Groups["token"].Value); + Match m = reg.Match(sorce); + System.Diagnostics.Debug.WriteLine("authenticity_token: " + m.Groups["token"].Value); - string url = "https://api.twitter.com/oauth/authorize"; + string url = "https://api.twitter.com/oauth/authorize"; - wc = new System.Net.WebClient(); + wc = new System.Net.WebClient(); - System.Collections.Specialized.NameValueCollection ps = - new System.Collections.Specialized.NameValueCollection(); + System.Collections.Specialized.NameValueCollection ps = + new System.Collections.Specialized.NameValueCollection(); - ps.Add("authenticity_token", m.Groups["token"].Value); - ps.Add("oauth_token", OAuthToken); - ps.Add("session[username_or_email]", ScreenName); - ps.Add("session[password]", Password); + ps.Add("authenticity_token", m.Groups["token"].Value); + ps.Add("oauth_token", OAuthToken); + ps.Add("session[username_or_email]", ScreenName); + ps.Add("session[password]", Password); - byte[] resData = wc.UploadValues(url, ps); - wc.Dispose(); + byte[] resData = wc.UploadValues(url, ps); + wc.Dispose(); - string resText = System.Text.Encoding.UTF8.GetString(resData); + string resText = System.Text.Encoding.UTF8.GetString(resData); - reg = new Regex("(?.*?)", - RegexOptions.IgnoreCase | RegexOptions.Singleline); + reg = new Regex("(?.*?)", + RegexOptions.IgnoreCase | RegexOptions.Singleline); - m = reg.Match(resText); - Console.WriteLine("pin: " + m.Groups["pin"].Value); + m = reg.Match(resText); + System.Diagnostics.Debug.WriteLine("pin: " + m.Groups["pin"].Value); - return await GetAccessTokenFromPinCode(m.Groups["pin"].Value); + return await GetAccessTokenFromPinCode(m.Groups["pin"].Value); + } + catch (Exception e) + { + return null; + } } /// diff --git a/Vermillion/Utility/RandomText.cs b/Vermillion/Utility/RandomText.cs new file mode 100644 index 0000000..9286fe8 --- /dev/null +++ b/Vermillion/Utility/RandomText.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Twitch.Vermillion.Utility +{ + public static class RandomText + { + } +}