OSDN Git Service

HttpClientでOAuth Echoを使用するためのHttpMessageHandlerを実装
authorKimura Youichi <kim.upsilon@bucyou.net>
Tue, 3 May 2016 12:51:38 +0000 (21:51 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Wed, 4 May 2016 08:40:12 +0000 (17:40 +0900)
OpenTween/Api/TwitterApi.cs
OpenTween/Connection/OAuthEchoHandler.cs [new file with mode: 0644]
OpenTween/Connection/TwitterApiConnection.cs
OpenTween/OpenTween.csproj

index 3362c8a..a5c134d 100644 (file)
@@ -465,6 +465,11 @@ namespace OpenTween.Api
             return this.apiConnection.GetStreamAsync(endpoint, param);
         }
 
+        public OAuthEchoHandler CreateOAuthEchoHandler(Uri authServiceProvider, Uri realm = null)
+        {
+            return ((TwitterApiConnection)this.apiConnection).CreateOAuthEchoHandler(authServiceProvider, realm);
+        }
+
         public void Dispose()
         {
             this.apiConnection?.Dispose();
diff --git a/OpenTween/Connection/OAuthEchoHandler.cs b/OpenTween/Connection/OAuthEchoHandler.cs
new file mode 100644 (file)
index 0000000..8ffc24b
--- /dev/null
@@ -0,0 +1,62 @@
+// OpenTween - Client of Twitter
+// Copyright (c) 2016 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
+// All rights reserved.
+//
+// This file is part of OpenTween.
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3 of the License, or (at your option)
+// any later version.
+//
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+// for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program. If not, see <http://www.gnu.org/licenses/>, or write to
+// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
+// Boston, MA 02110-1301, USA.
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Net.Http;
+using System.Net.Http.Headers;
+using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace OpenTween.Connection
+{
+    public class OAuthEchoHandler : DelegatingHandler
+    {
+        public Uri AuthServiceProvider { get; }
+        public string VerifyCredentialsAuthorization { get; }
+
+        public OAuthEchoHandler(HttpMessageHandler innerHandler, Uri authServiceProvider, string authorizationValue)
+            : base(innerHandler)
+        {
+            this.AuthServiceProvider = authServiceProvider;
+            this.VerifyCredentialsAuthorization = authorizationValue;
+        }
+
+        protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
+        {
+            request.Headers.Add("X-Auth-Service-Provider", this.AuthServiceProvider.AbsoluteUri);
+            request.Headers.Add("X-Verify-Credentials-Authorization", this.VerifyCredentialsAuthorization);
+
+            return base.SendAsync(request, cancellationToken);
+        }
+
+        public static OAuthEchoHandler CreateHandler(HttpMessageHandler innerHandler, Uri authServiceProvider,
+            string consumerKey, string consumerSecret, string accessToken, string accessSecret, Uri realm = null)
+        {
+            var credential = OAuthUtility.CreateAuthorization("GET", authServiceProvider, null,
+                consumerKey, consumerSecret, accessToken, accessSecret, realm?.AbsoluteUri);
+
+            return new OAuthEchoHandler(innerHandler, authServiceProvider, credential);
+        }
+    }
+}
index 724794c..33d7eb6 100644 (file)
@@ -236,6 +236,15 @@ namespace OpenTween.Connection
             }
         }
 
+        public OAuthEchoHandler CreateOAuthEchoHandler(Uri authServiceProvider, Uri realm = null)
+        {
+            var uri = new Uri(this.RestApiBase, authServiceProvider);
+
+            return OAuthEchoHandler.CreateHandler(Networking.CreateHttpClientHandler(), uri,
+                ApplicationSettings.TwitterConsumerKey, ApplicationSettings.TwitterConsumerSecret,
+                this.AccessToken, this.AccessSecret, realm);
+        }
+
         public void Dispose()
         {
             this.Dispose(true);
index 6cf8993..0f46a14 100644 (file)
     <Compile Include="Connection\LazyJson.cs" />
     <Compile Include="Connection\Mobypicture.cs" />
     <Compile Include="Connection\Networking.cs" />
+    <Compile Include="Connection\OAuthEchoHandler.cs" />
     <Compile Include="Connection\OAuthHandler.cs" />
     <Compile Include="Connection\OAuthUtility.cs" />
     <Compile Include="Connection\TwipplePhoto.cs" />