我在Twitter API讨论中发布了一个类似的问题,但没有收到回复。我正在尝试将近一周的时间从C#上的Twitter Streaming API公共流中获取地理定位推文但没有成功。以下是我正在使用的代码。当我用track参数更改location参数时,一切正常。但位置过滤器始终返回401错误。任何人都可以帮我解决我的代码吗?
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...
}
}
}
}
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...
}
}
}
}
提前致谢, Stefanos的
答案 0 :(得分:1)
我可以看到你遇到了一些与位置过滤相关的麻烦。 我遇到了同样的问题,但我终于得到了一些答案。
签名需要对逗号进行两次编码,将其转换为%252C。
该网址只应在将逗号转换为%2C后对其进行编码。
如果您希望查看tweetinvi.codeplex.com,其中包含FilteredStream,可以正确管理位置过滤器。
如果您查看魔法完成的OAuthWebRequestGenerator。
我希望这会对你有所帮助:)。