i have posted a similar question on Twitter API discussion but no reply received. I am trying almost a week to get geolocated tweets from Twitter Streaming API public stream on C# without success. Below is the code i am using. When i change the location parameter with the track parameter everything works fine. But the location filter always return a 401 error. Could anyone help me with my 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=" + 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, Stefanos