Пример #1
0
        public OAuthMessageHandler(string apiKey, string secret, string authToken, string authTokenSecret, IOAuthSignatureData provider)
        {
            _apiKey                = apiKey;
            _secret                = secret;
            _authToken             = authToken;
            _authTokenSecret       = authTokenSecret;
            _signatureDataProvider = provider;

            _hmacSha1Param  = new KeyValuePair <string, string>(Constants.oauth_signature_method, "HMAC-SHA1");
            _apiKeyParam    = new KeyValuePair <string, string>(Constants.oauth_consumer_key, _apiKey);
            _authTokenParam = new KeyValuePair <string, string>(Constants.oauth_token, _authToken);

            // Construct the OAuthVersion parameter based on the requested version.
            _oauthVersionParam = new KeyValuePair <string, string>(Constants.oauth_version, _signatureDataProvider.GetOAuthVersion());
            _keyBytes          = OAuthHelpers.CreateHashKeyBytes(_secret, _authTokenSecret);

            this.InnerHandler = new HttpClientHandler();
        }
Пример #2
0
        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());
        }