1

我在 Twitter API 讨论中发布了类似的问题,但没有收到回复。我尝试了将近一周的时间从 C# 上的 Twitter Streaming API 公共流中获取地理定位的推文,但没有成功。下面是我正在使用的代码。当我使用轨道参数更改位置参数时,一切正常。但是位置过滤器总是返回 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...
            }
        }


    }
}

在此先感谢,斯特凡诺斯

4

1 回答 1

1

我可以看到您在位置过滤方面遇到了一些麻烦。我遇到了同样的问题,但我终于得到了一些答案。

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

  • url 应仅在将逗号转换为 %2C 时对逗号进行编码。

如果您希望查看tweetinvi.codeplex.com,其中包含一个正确管理 Location 过滤器的FilteredStream

如果您查看完成魔术的OAuthWebRequestGenerator

我希望这能帮到您 :)

于 2013-10-08T16:10:04.583 回答