private string CreateRequest(string url, string method, string tokSecret, params object[] args) { SortedSet <KeyValuePair <string, string> > parameters = new SortedSet <KeyValuePair <string, string> >(new OAuthParameterComparer()) { new KeyValuePair <string, string>(Constants.oauth_consumer_key, _apiKey), new KeyValuePair <string, string>(Constants.oauth_nonce, OAuthHelpers.GenerateNonce()), new KeyValuePair <string, string>(Constants.oauth_timestamp, OAuthHelpers.GenerateTimestamp()), new KeyValuePair <string, string>(Constants.oauth_signature_method, "HMAC-SHA1"), }; if (!string.IsNullOrEmpty(_oauthVersion)) { parameters.Add(new KeyValuePair <string, string>(Constants.oauth_version, _oauthVersion)); } for (int i = 0; i < args.Length; i += 2) { parameters.Add(new KeyValuePair <string, string>(args[i].ToString(), args[i + 1].ToString())); } string baseString = OAuthHelpers.GenerateBaseString(url, method, parameters); string sig = OAuthHelpers.EncodeValue(OAuthHelpers.GenerateHMACDigest(baseString, OAuthHelpers.CreateHashKeyBytes(_secret, tokSecret))); parameters.Add(new KeyValuePair <string, string>(Constants.oauth_signature, sig)); StringBuilder sb = new StringBuilder(); foreach (var param in parameters) { sb.AppendFormat("{0}={1}&", param.Key, System.Net.WebUtility.HtmlEncode(param.Value)); } sb.Remove(sb.Length - 1, 1); return(sb.ToString()); }
private string GetAuthenticationHeaderForRequest(Uri requestUri, HttpMethod method, Dictionary <String, String> postvars) { List <KeyValuePair <string, string> > parameters = new List <KeyValuePair <string, string> >() { new KeyValuePair <string, string>(Constants.oauth_consumer_key, _apiKey), new KeyValuePair <string, string>(Constants.oauth_nonce, OAuthHelpers.GenerateNonce()), new KeyValuePair <string, string>(Constants.oauth_timestamp, OAuthHelpers.GenerateTimestamp()), new KeyValuePair <string, string>(Constants.oauth_signature_method, "HMAC-SHA1"), new KeyValuePair <string, string>(Constants.oauth_version, Constants.oauth_version_1a), new KeyValuePair <string, string>(Constants.oauth_token, _authToken), }; if (postvars != null) { foreach (var item in postvars) { parameters.Add(new KeyValuePair <string, string>(item.Key, item.Value)); } // reset parameters for future calls _parameters = null; } string baseUri = requestUri.OriginalString; // We need to handle the case where the request comes with query parameters if (!string.IsNullOrEmpty(requestUri.Query)) { baseUri = requestUri.OriginalString.Replace(requestUri.Query, ""); foreach (var param in requestUri.Query.Split(new char[] { '?', '&' }, StringSplitOptions.RemoveEmptyEntries)) { var values = param.Split('='); string name = values[0]; string value = string.Empty; if (values.Length > 1) { value = values[1]; } parameters.Add(new KeyValuePair <string, string>(Uri.UnescapeDataString(name), Uri.UnescapeDataString(value))); } } string baseString = OAuthHelpers.GenerateBaseString(baseUri, method.ToString(), parameters); string sig = OAuthHelpers.EncodeValue(OAuthHelpers.GenerateHMACDigest(baseString, _secret, _authTokenSecret)); parameters.Add(new KeyValuePair <string, string>(Constants.oauth_signature, sig)); StringBuilder sb = new StringBuilder(); foreach (var param in parameters) { if (param.Key.StartsWith("oauth")) { sb.AppendFormat("{0}={1},", param.Key, System.Net.WebUtility.HtmlEncode(param.Value)); } } sb.Remove(sb.Length - 1, 1); return(sb.ToString()); }
private async Task <string> GetAuthenticationHeaderForRequest(HttpRequestMessage request) { Uri requestUri = request.RequestUri; HttpMethod method = request.Method; List <KeyValuePair <string, string> > parameters = new List <KeyValuePair <string, string> >() { new KeyValuePair <string, string>(Constants.oauth_consumer_key, _apiKey), new KeyValuePair <string, string>(Constants.oauth_nonce, OAuthHelpers.GenerateNonce()), new KeyValuePair <string, string>(Constants.oauth_timestamp, OAuthHelpers.GenerateTimestamp()), new KeyValuePair <string, string>(Constants.oauth_signature_method, "HMAC-SHA1"), new KeyValuePair <string, string>(Constants.oauth_version, Constants.oauth_version_1a), new KeyValuePair <string, string>(Constants.oauth_token, _authToken), }; string baseUri = requestUri.OriginalString; // We need to handle the case where the request comes with query parameters, in URL or in body string queryString = string.Empty; if (!string.IsNullOrEmpty(requestUri.Query)) { baseUri = requestUri.OriginalString.Replace(requestUri.Query, ""); queryString = requestUri.Query; } if (request.Content.Headers.ContentType.MediaType == "application/x-www-form-urlencoded") { string requestContent = await request.Content.ReadAsStringAsync(); queryString = $"{queryString}&{requestContent}"; } foreach (var param in queryString.Split(new char[] { '?', '&' }, StringSplitOptions.RemoveEmptyEntries)) { var values = param.Split('='); string name = Uri.UnescapeDataString(values[0]); name = name.Replace('+', ' '); string value = string.Empty; if (values.Length > 1) { value = Uri.UnescapeDataString(values[1]); value = value.Replace('+', ' '); } parameters.Add(new KeyValuePair <string, string>(name, value)); } string baseString = OAuthHelpers.GenerateBaseString(baseUri, method.ToString(), parameters); string sig = OAuthHelpers.EncodeValue(OAuthHelpers.GenerateHMACDigest(baseString, _secret, _authTokenSecret)); parameters.Add(new KeyValuePair <string, string>(Constants.oauth_signature, sig)); StringBuilder sb = new StringBuilder(); foreach (var param in parameters) { if (param.Key.StartsWith("oauth")) { sb.AppendFormat("{0}={1},", param.Key, System.Net.WebUtility.HtmlEncode(param.Value)); } } sb.Remove(sb.Length - 1, 1); return(sb.ToString()); }