0

OAuth Unauthorized 401 in C#

I am trying to use Yahoo! Fantasy API to access fantasy baseball data for my C# project. To start I am testing this YQL query " select * from fantasysports.players where game_key='238' ".

When I run my code I get a 401 unauthorized.

I have seen in other forms that their consumer key and consumer secret have certain permissions. So can anyone tell me if the problem is my keys or in my code below..

Program.cs:

        static void Main(string[] args)
    {
            string yql = "select * from fantasysports.players where game_key='238'";
            Console.Write("1\n\n");
            //var xml = QueryYahoo(yql);
            //Console.Write(xml.InnerText);
            Console.Write("2\n\n");
            string consumerKey = "--My Consumer Key--";
            string consumerSecret = "--My Consumer Secret";

            var xml = QueryYahoo(yql, consumerKey, consumerSecret);
            Console.Write(xml.InnerText+ "\n\n");
        }

        private static XmlDocument QueryYahoo(string yql)
        {
            string url = "<http://query.yahooapis.com/v1/yql?q=select%20>*%20from%20fantasysports.games%20where%20game_key%3D%22238%22&diagnostics=true" + Uri.EscapeUriString(yql);

            var req = System.Net.HttpWebRequest.Create(url);
            var xml = new XmlDocument();
            using (var res = req.GetResponse().GetResponseStream())
            {
                xml.Load(res);
            }
            return xml;
        }

        private static XmlDocument QueryYahoo(string yql, string consumerKey, string consumerSecret)
        {
            string url = "<http://query.yahooapis.com/v1/yql?q=select%20>*%20from%20fantasysports.games%20where%20game_key%3D%22238%22&diagnostics=true" + Uri.EscapeUriString(yql);
            url = OAuth.GetUrl(url, consumerKey, consumerSecret);

            var req = System.Net.HttpWebRequest.Create(url);
            var xml = new XmlDocument();
            using (var res = req.GetResponse().GetResponseStream())
            {
                xml.Load(res);
            }
            return xml;
        }

Thanks!

by
1 Reply
  • Here is my Oauth.cs ~~~ public static class OAuth { private static Random Random = new Random(); private static string _UnreservedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-.~"; public const string HMACSHA1 = "HMAC-SHA1"; public static byte[] Download(string url, string consumerKey, string consumerSecret) { return Download(null, null, url, consumerKey, consumerSecret); } public static byte[] Download(string method, string hashAlgorithm, string url, string consumerKey, string consumerSecret) { url = GetUrl(method, hashAlgorithm, url, consumerKey, consumerSecret); var req = HttpWebRequest.Create(url); if (method != null) req.Method = method; using (var mem = new System.IO.MemoryStream()) using (var response = req.GetResponse()) { var stream = response.GetResponseStream(); int read = 1; byte[] buffer = new byte[8092]; while (read > 0) { read = stream.Read(buffer, 0, buffer.Length); mem.Write(buffer, 0, read); } return mem.ToArray(); } } public static string GetUrl(string url, string consumerKey, string consumerSecret) { return GetUrl(null, null, url, consumerKey, consumerSecret); } public static string GetUrl(string method, string hashAlgorithm, string url, string consumerKey, string consumerSecret) { method = method ?? "GET"; hashAlgorithm = hashAlgorithm ?? HMACSHA1; string timestamp = Math.Floor((DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds).ToString(); string nonce = GetRandomString(10); int index = url.IndexOf('?'); string querystring; if (index == -1) { querystring = string.Empty; } else { querystring = url.Substring(index + 1); url = url.Substring(0, index); } //parse the querystring into a dictionary, rather than NameValueCollection, so it's easier to manipulate Dictionary<string, string> query = querystring.Split('&').Select(x => { int i = x.IndexOf('='); if (i == -1) return new[] { x, null }; else return new[] { x.Substring(0, i), Uri.UnescapeDataString(x.Substring(i + 1)) }; }).ToDictionary(x => x[0], y => y[1]);

            //add the oauth stuffs
            query.Add("oauth_consumer_key", consumerKey);
            query.Add("oauth_nonce", nonce);
            query.Add("oauth_signature_method", "HMAC-SHA1");
            query.Add("oauth_timestamp", timestamp);
            query.Add("oauth_version", "1.0");
    
            //put the querystring back together in alphabetical order
            querystring = string.Join("&", query.OrderBy(x => x.Key).Select(x => string.Concat(x.Key, (x.Value == null ? string.Empty : "=" + x.Value.UrlEncode()))).ToArray());
    
            string data = string.Concat(method.ToUpper(), "&", url.UrlEncode(), "&", querystring.UrlEncode());
            string sig;
            using (var hasher = GetHashAglorithm(hashAlgorithm)) {
                hasher.Key = (consumerSecret + "&").GetBytes();
                sig = hasher.ComputeHash(data.GetBytes()).ToBase64();
            }
            return string.Concat(url, "?", querystring, "&oauth_signature=", sig.UrlEncode());
        }
    

    ~~~

    0

Recent Posts

in Pipes