From: Kimura Youichi Date: Tue, 3 May 2016 12:53:15 +0000 (+0900) Subject: ついっぷるフォトの投稿処理をOAuthEchoHandlerに移行 X-Git-Tag: OpenTween_v1.3.3~87^2~28 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=399fc231904d402de2cc5dc2f9fe28eee5f304e7;p=opentween%2Fopen-tween.git ついっぷるフォトの投稿処理をOAuthEchoHandlerに移行 --- diff --git a/OpenTween/Connection/TwipplePhoto.cs b/OpenTween/Connection/TwipplePhoto.cs index c52d4e09..2afa7a52 100644 --- a/OpenTween/Connection/TwipplePhoto.cs +++ b/OpenTween/Connection/TwipplePhoto.cs @@ -25,11 +25,13 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; +using System.Net.Http; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml; using System.Xml.Linq; using System.Xml.XPath; +using OpenTween.Api; using OpenTween.Api.DataModel; namespace OpenTween.Connection @@ -61,7 +63,7 @@ namespace OpenTween.Connection this.twitter = twitter; this.twitterConfig = twitterConfig; - this.twippleApi = new TwippleApi(twitter.AccessToken, twitter.AccessTokenSecret); + this.twippleApi = new TwippleApi(twitter.Api); } #endregion @@ -141,17 +143,21 @@ namespace OpenTween.Connection this.twitterConfig = config; } - public class TwippleApi : HttpConnectionOAuthEcho + public class TwippleApi { + private readonly HttpClient http; + private static readonly Uri UploadEndpoint = new Uri("http://p.twipple.jp/api/upload2"); - public TwippleApi(string twitterAccessToken, string twitterAccessTokenSecret) - : base(new Uri("http://api.twitter.com/"), new Uri("https://api.twitter.com/1.1/account/verify_credentials.json")) + private static readonly Uri OAuthRealm = new Uri("http://api.twitter.com/"); + private static readonly Uri AuthServiceProvider = new Uri("https://api.twitter.com/1.1/account/verify_credentials.json"); + + public TwippleApi(TwitterApi twitterApi) { - this.Initialize(ApplicationSettings.TwitterConsumerKey, ApplicationSettings.TwitterConsumerSecret, - twitterAccessToken, twitterAccessTokenSecret, "", ""); + var handler = twitterApi.CreateOAuthEchoHandler(AuthServiceProvider, OAuthRealm); - this.InstanceTimeout = 60000; + this.http = Networking.CreateHttpClient(handler); + this.http.Timeout = TimeSpan.FromMinutes(1); } /// @@ -163,25 +169,30 @@ namespace OpenTween.Connection { // 参照: http://p.twipple.jp/wiki/API_Upload2/ja - var param = new Dictionary + using (var request = new HttpRequestMessage(HttpMethod.Post, UploadEndpoint)) + using (var multipart = new MultipartFormDataContent()) { - ["upload_from"] = Application.ProductName, - }; - var paramFiles = new List> - { - new KeyValuePair("media", item), - }; - var response = ""; - - var uploadTask = Task.Run(() => this.GetContent(HttpConnection.PostMethod, - UploadEndpoint, param, paramFiles, ref response, null, null)); - - var ret = await uploadTask.ConfigureAwait(false); - - if (ret != HttpStatusCode.OK) - throw new WebApiException("Err:" + ret, response); - - return XDocument.Parse(response); + request.Content = multipart; + + using (var uploadFromContent = new StringContent(Application.ProductName)) + using (var mediaStream = item.OpenRead()) + using (var mediaContent = new StreamContent(mediaStream)) + { + multipart.Add(uploadFromContent, "upload_from"); + multipart.Add(mediaContent, "media", item.Name); + + using (var response = await this.http.SendAsync(request).ConfigureAwait(false)) + { + var responseText = await response.Content.ReadAsStringAsync() + .ConfigureAwait(false); + + if (!response.IsSuccessStatusCode) + throw new WebApiException(response.StatusCode.ToString(), responseText); + + return XDocument.Parse(responseText); + } + } + } } } } diff --git a/OpenTween/Twitter.cs b/OpenTween/Twitter.cs index 205b7272..a4b31e85 100644 --- a/OpenTween/Twitter.cs +++ b/OpenTween/Twitter.cs @@ -144,6 +144,7 @@ namespace OpenTween /// public static readonly Regex DMSendTextRegex = new Regex(@"^DM? +(?[a-zA-Z0-9_]+) +(?.*)", RegexOptions.IgnoreCase | RegexOptions.Singleline); + public TwitterApi Api { get; } public TwitterConfiguration Configuration { get; private set; } delegate void GetIconImageDelegate(PostClass post); @@ -168,7 +169,6 @@ namespace OpenTween //private FavoriteQueue favQueue; private HttpTwitter twCon = new HttpTwitter(); - private TwitterApi Api { get; } //private List _deletemessages = new List();