AsyncOAuth
AsyncOAuth copied to clipboard
Url with a space or special characters
I'm using the library with success , but if my Url has a space or a special character then my API provider gives me an unautorized
https://api-content.meocloud.pt/1/Metadata/meocloud/novapasta OK
https://api-content.meocloud.pt/1/Metadata/meocloud/nova com espaco/ NOK - UNAUTHORIZED
https://api-content.meocloud.pt/1/Metadata/meocloud/cão/
hmn, Why don't you escape a URL ?
Uri.EscapeUriString("https://api-content.meocloud.pt/1/Metadata/meocloud/nova com espaco/")
That was my first attempt :(
client = OAuthUtility.CreateOAuthClient(ConsumerKey, ConsumerSecret, new AccessToken(AccessToken, AccessTokenSecret));
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, new Uri(Uri.EscapeUriString("https://api-content.meocloud.pt/1/Metadata/meocloud/nova com espaco/"), UriKind.RelativeOrAbsolute)); var response = await client.SendAsync(request);
(I hard coded the url for demostration purpose... )
たぶん同じことが起きて、現物合わせで直ってるっぽくなったのでこちらに書かせていただきます。 自作Twitterクライアントにて使わせていただいているのですが、 searchAPIに「#セイレン filter:images」を投げようとすると認証が失敗する。 でも「#セイレン」や「#seiren filter:images」はちゃんと成功する、というわけのわかんないトラブルに遭遇しました。 同じことをTwitter API Consoleで試すと問題ないので調べたらsignature base stringがいけないことが判りました。 (GoogleCode OAuthBase.csのGenerateSignatureBase()経由にすると認証が通ったので比較した)
でいけない原因は
リクエストするurl(パラメーター) q=%23%E3%82%BB%E3%82%A4%E3%83%AC%E3%83%B3%20filter%3Aimages&result_type=recent&count=100
OAuthMessageHandlerのSendAsyncオーバーライドのHttpRequestMessage RequestUri.OrigianlString https://api.twitter.com/1.1/search/tweets.json?q=%23%E3%82%BB%E3%82%A4%E3%83%AC%E3%83%B3%20filter%3Aimages&result_type=recent&count=100 リクエストと同じ
OAuthUtility.GenerateSignatureのuriパラメーター OriginalString https://api.twitter.com/1.1/search/tweets.json?q=%23%E3%82%BB%E3%82%A4%E3%83%AC%E3%83%B3%20filter%3Aimages&result_type=recent&count=100 リクエストと同じ
Utility.ParseQueryStringのqueryパラメーター ?q=%23%E3%82%BB%E3%82%A4%E3%83%AC%E3%83%B3%20filter:images&result_type=recent&count=100 %3Aが:に化けて渡ってくる しかし?q=%23seiren%20filter%3Aimages&result_type=recent&count=100だと化けない そして化けた方でメッセージダイジェストを作らないと認証が失敗する
と、原因が掴めたけど理由がまだ判っていません。
以下のように変更したら動作したので暫定対策としました。
Utility.ParseQueryString()
else
{
//return new KeyValuePair<string , string>( xs[0].UrlDecode() , xs[1].UrlDecode() );
return new KeyValuePair<string , string>( xs[0] , xs[1] );
}
OAuthUtility.GenerateSignature()
var stringParameter = parameters.Where( x => x.Key.ToLower() != "realm" )
.Concat( queryParams )
//.Select( p => new { Key = p.Key.UrlEncode() , Value = p.Value.UrlEncode() } )
.OrderBy( p => p.Key , StringComparer.Ordinal )
.ThenBy( p => p.Value , StringComparer.Ordinal )
.Select( p => p.Key + "=" + p.Value )
.ToString( "&" );