From 357d7e80e2e7222cf25056a87606a02f23587f64 Mon Sep 17 00:00:00 2001 From: Kimura Youichi Date: Wed, 4 May 2016 16:52:42 +0900 Subject: [PATCH] =?utf8?q?yfrog=E3=81=AE=E6=8A=95=E7=A8=BF=E5=87=A6?= =?utf8?q?=E7=90=86=E3=82=92OAuthEchoHandler=E3=81=AB=E7=A7=BB=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- OpenTween/Connection/yfrog.cs | 61 +++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 25 deletions(-) diff --git a/OpenTween/Connection/yfrog.cs b/OpenTween/Connection/yfrog.cs index 2864b948..80dc76c5 100644 --- a/OpenTween/Connection/yfrog.cs +++ b/OpenTween/Connection/yfrog.cs @@ -30,10 +30,12 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; +using System.Net.Http; using System.Threading.Tasks; using System.Xml; using System.Xml.Linq; using System.Xml.XPath; +using OpenTween.Api; using OpenTween.Api.DataModel; namespace OpenTween.Connection @@ -53,7 +55,7 @@ namespace OpenTween.Connection this.tw = twitter; this.twitterConfig = twitterConfig; - this.yfrogApi = new YfrogApi(twitter.AccessToken, twitter.AccessTokenSecret); + this.yfrogApi = new YfrogApi(twitter.Api); } public int MaxMediaCount @@ -124,17 +126,21 @@ namespace OpenTween.Connection this.twitterConfig = config; } - public class YfrogApi : HttpConnectionOAuthEcho + public class YfrogApi { + private readonly HttpClient http; + private static readonly Uri UploadEndpoint = new Uri("https://yfrog.com/api/xauth_upload"); - public YfrogApi(string twitterAccessToken, string twitterAccessTokenSecret) - : base(new Uri("http://api.twitter.com/"), new Uri("https://api.twitter.com/1.1/account/verify_credentials.xml")) + 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 YfrogApi(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); } /// @@ -146,25 +152,30 @@ namespace OpenTween.Connection { // 参照: http://twitter.yfrog.com/page/api#a1 - var param = new Dictionary + using (var request = new HttpRequestMessage(HttpMethod.Post, UploadEndpoint)) + using (var multipart = new MultipartFormDataContent()) { - ["key"] = ApplicationSettings.YfrogApiKey, - }; - 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 apiKeyContent = new StringContent(ApplicationSettings.YfrogApiKey)) + using (var mediaStream = item.OpenRead()) + using (var mediaContent = new StreamContent(mediaStream)) + { + multipart.Add(apiKeyContent, "key"); + 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); + } + } + } } } } -- 2.11.0