C# 401 Twitter搜索位置筛选器中出现错误

C# 401 Twitter搜索位置筛选器中出现错误,c#,twitter,twitter-streaming-api,C#,Twitter,Twitter Streaming Api,我在Twitter API讨论上发布了一个类似的问题,但没有收到回复。我尝试了将近一周的时间从C#上的Twitter流媒体API公共流获取地理定位推文,但没有成功。下面是我正在使用的代码。当我用“轨迹”参数更改“位置”参数时,一切正常。但是位置过滤器总是返回401错误。有人能帮我写代码吗? 使用系统; 使用System.Collections.Generic; 使用System.IO; 使用System.Linq; Net系统; 使用System.Security.Cryptography;

我在Twitter API讨论上发布了一个类似的问题,但没有收到回复。我尝试了将近一周的时间从C#上的Twitter流媒体API公共流获取地理定位推文,但没有成功。下面是我正在使用的代码。当我用“轨迹”参数更改“位置”参数时,一切正常。但是位置过滤器总是返回401错误。有人能帮我写代码吗?

使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
Net系统;
使用System.Security.Cryptography;
使用系统文本;
使用System.Threading.Tasks;
命名空间TwitterPublicStreaming2
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串位置=“-180,-90180,90”;
字符串postBody=“?位置=“+位置;
字符串oauth_consumer_key=“”;
string oauth_nonce=Convert.ToBase64String(new ascienceoding().GetBytes(DateTime.Now.Ticks.ToString());
字符串oauth_signature_method=“HMAC-SHA1”;
字符串oauth_token=“”;
TimeSpan ts=DateTime.UtcNow—新的日期时间(1970,1,1,0,0,0);
字符串oauth_timestamp=Convert.ToInt64(ts.TotalSeconds.ToString();
字符串oauth_version=“1.0”;
SortedDictionary sd=新的SortedDictionary();
sd.添加(“位置”,位置);
添加(“oauth_版本”,oauth_版本);
添加(“oauth_消费者_密钥”,oauth_消费者_密钥);
sd.添加(“oauth_nonce”,oauth_nonce);
添加(“oauth_签名方法”,oauth_签名方法);
添加(“oauth_时间戳”,oauth_时间戳);
添加(“oauth_令牌”,oauth_令牌);
string baseString=string.Empty;
baseString+=“POST&”;
baseString+=Uri.EscapeDataString(“https://stream.twitter.com/1.1/statuses/filter.json") + "&";
foreach(sd中的KeyValuePair条目)
{
baseString+=Uri.EscapeDataString(entry.Key+“=”+entry.Value+“&”);
}
baseString=baseString.Substring(0,baseString.Length-3);
字符串consumerSecret=“”;
字符串oauth_token_secret=“”;
string signingKey=Uri.EscapeDataString(ConsumerCret)+“&”+Uri.EscapeDataString(oauth_token_secret);
HMACSHA1 hasher=new-HMACSHA1(new-ascienceoding().GetBytes(signingKey));
string signatureString=Convert.ToBase64String(hasher.ComputeHash(new-ascienceoding().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\u nonce=“+”\”+
EscapeDataString(oauth\u nonce)+“\”,“;
授权标题参数+=
“oauth\u签名\u方法=“+”\”+
EscapeDataString(oauth\u签名\u方法)+
"\",";
authorizationHeaderParams+=“oauth\u timestamp=“+”\”+
EscapeDataString(oauth\u时间戳)+“\”,“;
authorizationHeaderParams+=“oauth\u消费者\u密钥=”
+“\”+Uri.EscapeDataString(
oauth(用户密钥)+“\”,“;
authorizationHeaderParams+=“oauth\u令牌=“+”\”+
EscapeDataString(oauth\u令牌)+“\”,“;
authorizationHeaderParams+=“oauth\u签名=“+”\”
+EscapeDataString(signatureString)+“\”,“;
authorizationHeaderParams+=“oauth\u version=“+”\”+
EscapeDataString(oauth\u版本)+“\”;
hwr.Headers.Add(
“授权”,授权标题参数);
hwr.Method=“POST”;
hwr.ContentType=“应用程序/x-www-form-urlencoded”;
Stream=hwr.GetRequestStream();
字节[]体字节=
新的ascienceoding().GetBytes(postBody);
stream.Write(bodyBytes,0,bodyBytes.Length);
stream.Flush();
stream.Close();
硬件超时=3*60*1000;
尝试
{
HttpWebResponse rsp=hwr.GetResponse()
作为HttpWebResponse;
Console.WriteLine(“最终”);
//GS-在这里做点什么。。。
}
捕获(WebE例外)
{
控制台。写线(“该死”);
//在这里做一些聪明的错误处理。。。
}
}
}
}

提前感谢,,
Stefanos

我可以看出您在位置筛选方面遇到了一些问题。 我面临着同样的问题,但我最终得到了一些答案

  • 签名需要对逗号编码两次,将其转换为%252C。

  • url只应将逗号编码一次,并将其转换为%2C。

如果您希望查看其中包含正确管理位置筛选器的

如果你有一个魔术是在哪里做的


我希望这将对您有所帮助:)

我可以看出您在位置筛选方面遇到了一些问题。 我面临着同样的问题,但我最终得到了一些答案

  • 签名需要对逗号编码两次,将其转换为%252C。

  • url只应将逗号编码一次,并将其转换为%2C。

如果您希望查看其中包含正确管理位置筛选器的

如果你有一个魔术是在哪里做的

我希望这能帮助你:)

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...
            }
        }


    }
}