private string GenerateSignature(Uri uri, MethodType methodType, Token token, IEnumerable<KeyValuePair<string, object>> parameters) { var hmacKeyBase = ConsumerSecret.UrlEncode() + "&" + ((token == null) ? "" : token.Secret).UrlEncode(); using (var hmacsha1 = new HMACSHA1(Encoding.UTF8.GetBytes(hmacKeyBase))) { var stringParameter = parameters.OrderBy(p => p.Key) .ThenBy(p => p.Value) .ToQueryParameter(); var signatureBase = methodType.ToString().ToUpper() + "&" + uri.GetComponents(UriComponents.SchemeAndServer | UriComponents.Path, UriFormat.Unescaped).UrlEncode() + "&" + stringParameter.UrlEncode(); var hash = hmacsha1.ComputeHash(Encoding.UTF8.GetBytes(signatureBase)); return Convert.ToBase64String(hash).UrlEncode(); } }
protected IDictionary<string,object> ConstructBasicParameters(string url, MethodType methodType, Token token, params KeyValuePair<string,object>[] optionalParameters) { Enforce.NotNull(url, "url"); Enforce.NotNull(optionalParameters, "optionalParameters"); var parameters = new Dictionary<string, object> { { "oauth_consumer_key", ConsumerKey }, { "oauth_nonce", _random.Next() }, { "oauth_timestamp", DateTime.UtcNow.ToUnixTime() }, { "oauth_signature_method", "HMAC-SHA1" }, { "oauth_version", "1.0" } }; if (token != null) parameters.Add("oauth_token", token.Key); var signature = GenerateSignature(new Uri(url), methodType, token, parameters.Concat(optionalParameters)); parameters.Add("oauth_signature", signature); return parameters; }