Beispiel #1
0
        public async Task <string> buscarAlimento(string alimento)
        {
            const string baseURL = "http://platform.fatsecret.com/rest/server.api?";

            const string consumerKey     = "89a75f2dda9748d08cc87d573992fecb";
            string       signatureMethod = "HMAC-SHA1";
            string       timestamp       = OAuthTools.GetTimestamp();
            string       nonce           = OAuthTools.GetNonce();
            const string version         = "1.0";
            const string method          = "foods.search";
            const string format          = "json";


            var normalizedParameters = "format=" + format
                                       + "&method=" + method
                                       + "&oauth_consumer_key=" + consumerKey
                                       + "&oauth_nonce=" + nonce
                                       + "&oauth_signature_method=" + signatureMethod
                                       + "&oauth_timestamp=" + timestamp
                                       + "&oauth_version=" + version
                                       + "&search_expression=" + "banana";

            var httpMethod = "GET";
            var encodedUri = OAuthTools.UrlEncodeStrict(baseURL.Substring(0, baseURL.Length - 1));
            var encodedNormalizedParameters = OAuthTools.UrlEncodeStrict(normalizedParameters);
            var baseSignature = httpMethod + "&" + encodedUri + "&" + encodedNormalizedParameters;

            string signature;

            using (HMACSHA1 hmac = new HMACSHA1(Encoding.ASCII.GetBytes("5e1b6dc6e9e84451bc265d633b9fc0de&")))
            {
                byte[] hashPayLoad = hmac.ComputeHash(Encoding.ASCII.GetBytes(baseSignature));
                signature = Convert.ToBase64String(hashPayLoad);
            }

            string fullURL = baseURL + normalizedParameters
                             + "&oauth_signature=" + signature;

            String responseText = await Task.Run(() =>
            {
                try
                {
                    HttpWebRequest request = WebRequest.Create(fullURL) as HttpWebRequest;
                    request.Method         = "GET";
                    WebResponse response   = request.GetResponse();
                    Stream responseStream  = response.GetResponseStream();
                    return(new StreamReader(responseStream).ReadToEnd());
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error: " + e.Message);
                }

                return(null);
            });

            return(responseText);
        }
Beispiel #2
0
        public void Can_strict_url_encode_complex_string()
        {
            const string expected = "%21%3F%22%3B%3A%3C%3E%5C%5C%7C%60%23%24%25%5E%26%2A%2B-_%7B%7D%5B%5D";
            const string sequence = @"!?"";:<>\\|`#$%^&*+-_{}[]";

            var actual = OAuthTools.UrlEncodeStrict(sequence);

            Assert.AreEqual(expected, actual);
        }
        public void Can_strict_prevent_double_encoding()
        {
            const string expected = "123AaBb%21%3F%22%3B%3A%3C%3E%5C%5C%7C%60%23%24%25%20%25A%5E%26%2A%2B-_%7B%7D%5B%5D%F0%9F%98%83";
            const string sequence = @"123AaBb!?"";:<>\\|`#$% %A^&*+-_{}[]😃";

            var actual = OAuthTools.UrlEncodeStrict(sequence);

            actual = OAuthTools.UrlEncodeStrict(actual);
            Assert.AreEqual(expected, actual);
        }
        public static string ToQueryString(this IDictionary <string, string> collection)
        {
            var sb = new StringBuilder();

            foreach (var key in collection.Keys)
            {
                var parameter = OAuthTools.UrlEncodeStrict(collection[key]);
                sb.AppendFormat("{0}={1}&", key, parameter);
            }
            return(sb.ToString().TrimEnd('&'));
        }
Beispiel #5
0
        private string GetQueryString(Dictionary <string, string> dict)
        {
            if (dict == null)
            {
                return("");
            }
            List <string> sb = new List <string>();

            foreach (var kvp in dict)
            {
                sb.Add(string.Format("{0}={1}", OAuthTools.UrlEncodeStrict(kvp.Key), OAuthTools.UrlEncodeStrict(kvp.Value)));
            }
            return(string.Join("&", sb));
        }
Beispiel #6
0
        public void Authenticate_ShouldAllowEmptyConsumerSecret_OnHttpAuthorizationHeaderHandling(OAuthType type)
        {
            // Arrange
            const string url = "https://no-query.string";

            var client  = new RestClient(url);
            var request = new RestRequest();

            _authenticator.Type           = type;
            _authenticator.ConsumerSecret = null;

            // Act
            _authenticator.Authenticate(client, request);

            // Assert
            var authParameter = request.Parameters.Single(x => x.Name == "Authorization");
            var value         = (string)authParameter.Value;

            Assert.IsNotEmpty(value);
            Assert.IsTrue(value !.Contains("OAuth"));
            Assert.IsTrue(value.Contains("oauth_signature=\"" + OAuthTools.UrlEncodeStrict("&")));
        }
Beispiel #7
0
        public void UrlStrictEncode_Encodes_Correctly(string value, string expected)
        {
            var actual = OAuthTools.UrlEncodeStrict(value);

            Assert.Equal(expected, actual);
        }
 public void UrlEncodeStrict_Throws_ArgumentNull_On_Null_String()
 {
     Assert.Throws <ArgumentNullException>(() => OAuthTools.UrlEncodeStrict(null));
 }
        public void UrlEncodeStrict_Encodes_Uri_Properly(string uri, string expected)
        {
            string encodedUri = OAuthTools.UrlEncodeStrict(uri);

            Assert.Equal(expected, encodedUri);
        }