Skip to content

Instantly share code, notes, and snippets.

@mntone
Last active December 20, 2015 01:19
Show Gist options
  • Save mntone/6048599 to your computer and use it in GitHub Desktop.
Save mntone/6048599 to your computer and use it in GitHub Desktop.
AsyncOAuth を無理やり Windows.Net.Http 空間でも使えるように変換をかけて使うもの。Windows 8.1 Preview で UserStream を正常稼働させるのに必要。 なお、以下のコードを実行すればよい: private static HttpClient CreateClient( Account a ) { var baseFilter = new HttpBaseProtocolFilter(); baseFilter.AutomaticDecompression = true; baseFilter.IgnorableServerCertificateErrors.Add( ChainValidationResult.Untrusted );…
using AsyncOAuth;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Web.Http;
using Windows.Web.Http.Headers;
using Windows.Web.Http.Filters;
namespace Mntone.Critica.WindowsStore.Models.Communication
{
public class OAuthFilter: IHttpFilter
{
private readonly IHttpFilter _filter;
private readonly string _consumerKey, _consumerSecret;
private readonly Token _token;
private readonly IEnumerable<KeyValuePair<string, string>> _parameters;
public OAuthFilter(
string consumerKey,
string consumerSecret,
Token token = null,
IEnumerable<KeyValuePair<string, string>> optionalOAuthHeaderParameters = null ):
this( new HttpBaseProtocolFilter(), consumerKey, consumerSecret, token, optionalOAuthHeaderParameters )
{
}
public OAuthFilter(
IHttpFilter filter,
string consumerKey,
string consumerSecret,
Token token = null,
IEnumerable<KeyValuePair<string, string>> optionalOAuthHeaderParameters = null )
{
_filter = filter;
_consumerKey = consumerKey;
_consumerSecret = consumerSecret;
_token = token;
_parameters = optionalOAuthHeaderParameters ?? Enumerable.Empty<KeyValuePair<string, string>>();
}
public void Dispose()
{
GC.SuppressFinalize( this );
}
public IAsyncOperationWithProgress<HttpResponseMessage, HttpProgress> SendRequestAsync( HttpRequestMessage request )
{
var sendParameter = _parameters;
if( request.Method.Method == "POST" )
{
// form url encoded content
if( request.Content is HttpFormUrlEncodedContent )
{
var extraParameter = request.Content.ReadAsStringAsync().GetResults();
var parsed = new WwwFormUrlDecoder( extraParameter );
sendParameter = sendParameter.Concat( parsed.Select( x => new KeyValuePair<string, string>( x.Name, x.Value ) ) );
}
}
var headerParams = OAuthUtility.BuildBasicParameters(
_consumerKey, _consumerSecret, request.RequestUri.OriginalString, ConvertWindowsToSystem( request.Method ), _token, sendParameter );
headerParams = headerParams.Concat( _parameters );
var header = string.Join( ",", headerParams.Select( p => p.Key + "=\"" + p.Value + "\"" ) );
request.Headers.Authorization = new HttpCredentialsHeaderValue( "OAuth", header );
return _filter.SendRequestAsync( request );
}
private System.Net.Http.HttpMethod ConvertWindowsToSystem( HttpMethod method )
{
System.Net.Http.HttpMethod oMethod;
if( method.Method == "DELETE" ) oMethod = System.Net.Http.HttpMethod.Delete;
else if( method.Method == "GET" ) oMethod = System.Net.Http.HttpMethod.Get;
else if( method.Method == "HEAD" ) oMethod = System.Net.Http.HttpMethod.Head;
else if( method.Method == "OPTIONS" ) oMethod = System.Net.Http.HttpMethod.Options;
else if( method.Method == "POST" ) oMethod = System.Net.Http.HttpMethod.Post;
else if( method.Method == "PUT" ) oMethod = System.Net.Http.HttpMethod.Put;
else throw new ArgumentException();
return oMethod;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment