401 on location filter in C#


#1

Hi to all,

i have created a C# console application in order to retrieve geolocated tweets from the Twitter Streaming API. I have followed that link: http://garyshortblog.wordpress.com/2011/02/11/a-twitter-oauth-example-in-c/ to implement it. However, when i change the status parameter with the locations i always have a 401 error. On the other hand, if i filter my request using the track parameter everything works fine.Is there any specific request which i should follow? Here is my actual code.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace TwitterPublicStreaming2
{
    class Program
    {
        static void Main(string[] args)
        {
            string location = "-180,-90,180,90";
            string postBody = "locations=" + Uri.EscapeDataString(location);

            string oauth_consumer_key = "";
            string oauth_nonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));

            string oauth_signature_method = "HMAC-SHA1";
            string oauth_token = "";


            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);

            string oauth_timestamp = Convert.ToInt64(ts.TotalSeconds).ToString();

            string oauth_version = "1.0";


            SortedDictionary<string, string> sd = new SortedDictionary<string, string>();
            sd.Add("locations", location);
            sd.Add("oauth_version", oauth_version);
            sd.Add("oauth_consumer_key", oauth_consumer_key);
            sd.Add("oauth_nonce", oauth_nonce);
            sd.Add("oauth_signature_method", oauth_signature_method);
            sd.Add("oauth_timestamp", oauth_timestamp);
            sd.Add("oauth_token", oauth_token);

            string baseString = String.Empty;
            baseString += "POST&";
            baseString += Uri.EscapeDataString("https://stream.twitter.com/1.1/statuses/filter.json") + "&";


            foreach (KeyValuePair<string, string> entry in sd)
            {
                baseString += Uri.EscapeDataString(entry.Key + "=" + entry.Value + "&");
            }


            baseString = baseString.Substring(0, baseString.Length - 3);

            string consumerSecret = "";
            string oauth_token_secret = "";

            string signingKey = Uri.EscapeDataString(consumerSecret) + "&" + Uri.EscapeDataString(oauth_token_secret);

            HMACSHA1 hasher = new HMACSHA1(new ASCIIEncoding().GetBytes(signingKey));

            string signatureString = Convert.ToBase64String(hasher.ComputeHash(new ASCIIEncoding().GetBytes(baseString)));

            ServicePointManager.Expect100Continue = false;

            HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(@"https://stream.twitter.com/1.1/statuses/filter.json");


            string authorizationHeaderParams = String.Empty;
            authorizationHeaderParams += "OAuth ";
            authorizationHeaderParams += "oauth_nonce=" + "\"" +
                Uri.EscapeDataString(oauth_nonce) + "\",";

            authorizationHeaderParams +=
            "oauth_signature_method=" + "\"" +
            Uri.EscapeDataString(oauth_signature_method) +
            "\",";

            authorizationHeaderParams += "oauth_timestamp=" + "\"" +
           Uri.EscapeDataString(oauth_timestamp) + "\",";

            authorizationHeaderParams += "oauth_consumer_key="
                + "\"" + Uri.EscapeDataString(
                oauth_consumer_key) + "\",";

            authorizationHeaderParams += "oauth_token=" + "\"" +
                Uri.EscapeDataString(oauth_token) + "\",";

            authorizationHeaderParams += "oauth_signature=" + "\""
                + Uri.EscapeDataString(signatureString) + "\",";

            authorizationHeaderParams += "oauth_version=" + "\"" +
                Uri.EscapeDataString(oauth_version) + "\"";


            hwr.Headers.Add(
           "Authorization", authorizationHeaderParams);

            hwr.Method = "POST";
            hwr.ContentType = "application/x-www-form-urlencoded";
            Stream stream = hwr.GetRequestStream();
            byte[] bodyBytes =
                new ASCIIEncoding().GetBytes(postBody);

            stream.Write(bodyBytes, 0, bodyBytes.Length);
            stream.Flush();
            stream.Close();

            hwr.Timeout = 3 * 60 * 1000;

            try
            {
                HttpWebResponse rsp = hwr.GetResponse()
                    as HttpWebResponse;
                Console.WriteLine("Finally");
                //GS - Do something with the return here...
            }
            catch (WebException e)
            {
                Console.WriteLine("Damn");
                //GS - Do some clever error handling here...
            }
        }


    }
}

Thanks in advance


#2

Any answer ?!


#3

I’d take a look at how those comma characters are being encoded in the query string and the signature base string of your OAuth. You might have some double or triple escaping going on. Compare your base string you create with the one that OAuth signature tool creates.