title: 401 error from Twitter Stream API using C#
body: <p>I get 401 when i tried to convert it for the stream api: "<a href="https://stream.twitter.com/1/statuses/filter.json" rel="nofollow">https://stream.twitter.com/1/statuses/filter.json</a>". I will really appreciate any help. See code below:
  <code></p>
  
  <pre>            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}&amp;oauth_nonce={1}&amp;oauth_signature_method={2}" +
                                       "&amp;oauth_timestamp={3}&amp;oauth_token={4}&amp;oauth_version={5}";
                                       &amp;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&amp;", Uri.EscapeDataString(this.Datasource), "&amp;", Uri.EscapeDataString(baseString));
  
                  string compositeKey = string.Concat(Uri.EscapeDataString(oauth_consumer_secret), "&amp;", 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)
              {
                  if (wex.Status == WebExceptionStatus.ProtocolError)
                  {
                      /* as per twitter api
                       * 
                       * When a HTTP error (&gt; 200) is returned, back off exponentially.  
                       * Perhaps start with a 10 second wait, double on each subsequent failure,
                       * and finally cap the wait at 240 seconds. 
                       * 
                       * Exponential Backoff
                       */
                      if (backOff &lt; TwitterStreamProvider.InitialExponentialBackOff)
                      {
                          backOff = TwitterStreamProvider.InitialExponentialBackOff;
                      }
                      else
                      {
                          if (backOff &lt; TwitterStreamProvider.MaxExponentialBackOff)
                          {
                              backOff *= TwitterStreamProvider.ExponentialBackOff;
                          }
                      }
                  }
                  else
                  {
                      /* as per twitter api
                       * 
                       * When a network error (TCP/IP level) is encountered, back off linearly.  
                       * Perhaps start at 250 milliseconds and cap at 16 seconds.
                       * 
                       * Linear Backoff
                       */
                      if (backOff &lt; TwitterStreamProvider.MaxLinearBackOff)
                      {
                          backOff += TwitterStreamProvider.LinearBackOff;
                      }
                  }
  
                  Logger.Error(
                      string.Format("There was a problem trying to connect to the twitter stream.  Will try to establish connection again in {0} milliseconds",
                          backOff),
                      wex);
              }
              catch (OperationCanceledException cex)
              {
                  Logger.Error("Twitter stream thread was canceled", cex);
                  cancelled = true;
              }
              catch (Exception ex)
              {
                  Logger.Error("There was a problem while scraping twitter.", ex);
                  Logger.Debug(string.Format("Last tweet before the error: {0}", line));
                  throw;
              }
              finally
              {
                  if (request != null)
                  {
                      request.Abort();
                      request = null;
                  }
              }
  
              if (!cancelled)
              {
                  System.Threading.Thread.Sleep(backOff);
              }
          }
      }
  </code></pre>
  
  <p>/ </p>
  
  <p>Can you please point me into what am I doing wrong.</p>
  
  <p>Thanks,
  Marta</p>