Ejemplo n.º 1
0
        /// <summary>
        /// Calculates the message parameters, including the oaut_signature
        /// </summary>
        /// OAuthUtility.GetMessageParameters(apiKey, appSecret, url, method, accessToken, args);
        internal static string GetMessageParameters(string APIKey, string Secret, string url, string methodName, Token tok, params object[] args)
        {
            List <KeyValuePair <string, string> > parameters = new List <KeyValuePair <string, string> >()
            {
                new KeyValuePair <string, string>(oauth_timestamp, OAuthUtility.GenerateTimestamp()),
                new KeyValuePair <string, string>(oauth_nonce, OAuthUtility.GenerateNonce()),
                new KeyValuePair <string, string>(oauth_consumer_key, APIKey),
                new KeyValuePair <string, string>(oauth_signature_method, "HMAC-SHA1"),
                new KeyValuePair <string, string>(oauth_version, "1.0"),
                //new KeyValuePair<string,string>("Pretty", "true")
            };

            var method = new KeyValuePair <string, string>("method", methodName);

            parameters.Add(method);

            // add the access token if present
            if (tok != null)
            {
                parameters.Add(new KeyValuePair <string, string>(oauth_token, tok.id));
            }

            for (int i = 0; i < args.Length; i += 2)
            {
                parameters.Add(new KeyValuePair <string, string>(args[i].ToString(), args[i + 1].ToString()));
            }

            string baseString = OAuthUtility.GenerateBaseString(url, "GET", parameters);
            string sig        = OAuthUtility.EncodeValue(OAuthUtility.GenerateHMACDigest(baseString, Secret, tok == null ? "" : tok.Secret));

            parameters.Add(new KeyValuePair <string, string>(oauth_signature, sig));

            parameters.Remove(method);

            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("method={0}", methodName);

            foreach (var param in parameters)
            {
                if (param.Key.StartsWith("oauth_"))
                {
                    sb.AppendFormat("&{0}={1}", param.Key, HttpUtility.HtmlEncode(param.Value));
                }
                else
                {
                    sb.AppendFormat("&{0}={1}", param.Key, EncodeValue(param.Value));
                }
            }

            return(sb.ToString());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Calculates teh Authorization header to use with the oAuth authentication
        /// </summary>
        internal static string GetAuthorizationHeader(string apiKey, string appSecret, Token tok, string endpoint, params object[] args)
        {
            List <KeyValuePair <string, string> > parameters = new List <KeyValuePair <string, string> >()
            {
                new KeyValuePair <string, string>(oauth_timestamp, OAuthUtility.GenerateTimestamp()),
                new KeyValuePair <string, string>(oauth_nonce, OAuthUtility.GenerateNonce()),
                new KeyValuePair <string, string>(oauth_consumer_key, apiKey),
                new KeyValuePair <string, string>(oauth_signature_method, "HMAC-SHA1"),
                new KeyValuePair <string, string>(oauth_token, tok.id),
                new KeyValuePair <string, string>(oauth_token_secret, tok.Secret)
            };

            // Add of the other parameters to the mix
            for (int i = 0; i < args.Length; i += 2)
            {
                parameters.Add(new KeyValuePair <string, string>(args[i].ToString(), args[i + 1].ToString()));
            }

            string baseString = OAuthUtility.GenerateBaseString(endpoint, "PUT", parameters);
            string sig        = OAuthUtility.EncodeValue(OAuthUtility.GenerateHMACDigest(baseString, appSecret, tok == null ? "" : tok.Secret));

            parameters.Add(new KeyValuePair <string, string>(oauth_signature, sig));

            // the authorization header only consists of oauth_ tokens


            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("OAuth ");

            foreach (var param in parameters)
            {
                if (param.Key.StartsWith("oauth", StringComparison.OrdinalIgnoreCase))
                {
                    sb.AppendFormat("{0}=\"{1}\",", param.Key, param.Value);
                }
            }
            // remove the trailing ,
            sb.Remove(sb.Length - 1, 1);

            return(sb.ToString());
        }