401 error when streaming using OAuth, C#


#1

I get 401 when i tried to convert it for the stream api: “https://stream.twitter.com/1/statuses/filter.json” using OAuth. I will really appreciate any help. See code below:

         HttpWebRequest request = null;

        string oauth_consumer_key = "XXXXXXXXXXXXX";
        string oauth_consumer_secret = "XXXXXXXXXXXXXXXX";
        string oauth_token = "XXXXXXXXXXXXXXXXXXXXX";
        string oauth_token_secret = "XXXXXXXXXXXXXXXXXXXXXXXXX";
        string oauth_version = "1.0";
        string oauth_signature_method = "HMAC-SHA1";
        string oauth_nonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
        TimeSpan timeSpan = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
        string oauth_timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString();

        Encoding encoder = System.Text.Encoding.GetEncoding("utf-8");

        int backOff = 250;
        string line = string.Empty;

        try
        {
            string parameters = this.GetQuery(template);

            string baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" +
                                 "&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}";
                                 &track={6}"
            string baseString = string.Format(baseFormat,
                                                oauth_consumer_key,
                                                oauth_nonce,
                                                oauth_signature_method,
                                                oauth_timestamp,
                                                oauth_token,
                                                oauth_version,
                                                Uri.EscapeDataString(parameters)
                                                );

            baseString = string.Concat("POST&", Uri.EscapeDataString(this.Datasource), "&", Uri.EscapeDataString(baseString));

            string compositeKey = string.Concat(Uri.EscapeDataString(oauth_consumer_secret), "&", Uri.EscapeDataString(oauth_token_secret));

            string oauth_signature;
            using (HMACSHA1 hasher = new HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey)))
            {
                oauth_signature = Convert.ToBase64String(
                hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(baseString)));
            }

            // create the request header
            string headerFormat = "OAuth oauth_consumer_key=\"{0}\", oauth_nonce=\"{1}\", " + 
                                  "oauth_signature=\"{2}\", oauth_signature_method=\"{3}\", " +
                                  "oauth_timestamp=\"{4}\",  " +
                                  "oauth_token=\"{5}\", " +
                                  "oauth_version=\"{6}\"";

            string authHeader = string.Format(headerFormat,
                                            Uri.EscapeDataString(oauth_consumer_key),
                                            Uri.EscapeDataString(oauth_nonce),
                                            Uri.EscapeDataString(oauth_signature),
                                            Uri.EscapeDataString(oauth_signature_method),
                                            Uri.EscapeDataString(oauth_timestamp),
                                            Uri.EscapeDataString(oauth_token),
                                            Uri.EscapeDataString(oauth_version)
                                            );

            ServicePointManager.Expect100Continue = false;
            //string postBody = string.Format("track={0}", parameters);
            string postBody = "track=" + Uri.EscapeDataString(parameters);

            request = (HttpWebRequest)WebRequest.Create(this.Datasource + "?" + postBody);
            request.Headers.Add("Authorization", authHeader);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.PreAuthenticate = true;
            request.AllowWriteStreamBuffering = true;

            using (WebResponse response = request.GetResponse())
            {
               using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    string responseData = new StreamReader(response.GetResponseStream()).ReadToEnd();

                    //we successfully connected so reset the twitter backoff time

        }
        catch (WebException wex)
        {

}

    }
}


Can you please point me into what am I doing wrong.

Thanks