问题描述
我对此已经无能为力了,我已经查看了这里的所有内容,虽然有人问过与我类似的问题,但我还没有找到确切的答案.
I'm at my wit's end with this, I have looked all over here and while questions similar to mine have been asked, I can't find this exact one with any answers yet.
我一直在尝试调用 https://stream.twitter.com/1.1/statuses/filter.json 来自 C# 控制台应用程序中的几天,并不断收到 401 Unauthorized 错误,无论我做什么都不会消失.也适用于非流式传输的任何内容.
I have been trying to call https://stream.twitter.com/1.1/statuses/filter.json from within a C# console app for days now and keep getting at 401 Unauthorized error that no matter what I do won't go away. Works fine for anything not streaming as well.
const string streamUrl = "https://stream.twitter.com/1.1/statuses/filter.json";
const string oauth_token = "XXX-XXXXX";
const string oauth_token_secret = "XXXXXXXXXXXX";
const string oauth_consumer_key = "XXXXXXXXXXXXX";
const string oauth_consumer_secret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
// oauth implementation details
const string oauth_version = "1.0a";
const string oauth_signature_method = "HMAC-SHA1";
// unique request details
var oauth_nonce = Convert.ToBase64String(
new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
var timeSpan = DateTime.UtcNow
- new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
var oauth_timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString();
// create oauth signature
const string baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" +
"&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&track=twitter";
var baseString = string.Format(baseFormat,
oauth_consumer_key,
oauth_nonce,
oauth_signature_method,
oauth_timestamp,
oauth_token,
oauth_version);
string oauth_signature;
var compositeKey = string.Concat(Uri.EscapeDataString(oauth_consumer_secret),
"&", Uri.EscapeDataString(oauth_token_secret));
using (var hasher = new HMACSHA1(Encoding.ASCII.GetBytes(compositeKey)))
{
oauth_signature = Convert.ToBase64String(hasher.ComputeHash(Encoding.ASCII.GetBytes(baseString)));
}
// create the request header
const 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}\"";
var 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));
const string postparameters = "track=twitter";
streamUrl = string.Format("{0}?{1}", streamUrl, postparameters);
while (true)
{
var timeLineRequest = (HttpWebRequest)WebRequest.Create(streamUrl);
timeLineRequest.Headers.Add("Authorization", authHeader);
var encode = System.Text.Encoding.GetEncoding("utf-8");
ServicePointManager.Expect100Continue = false;
if (postparameters.Length > 0)
{
timeLineRequest.Method = "POST";
timeLineRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
byte[] _twitterTrack = encode.GetBytes(postparameters);
timeLineRequest.ContentLength = _twitterTrack.Length;
Stream _twitterPost = timeLineRequest.GetRequestStream();
_twitterPost.Write(_twitterTrack, 0, _twitterTrack.Length);
_twitterPost.Close();
}
var webResponse = (HttpWebResponse)timeLineRequest.GetResponse();
var responseStream = new StreamReader(webResponse.GetResponseStream(), encode);
while (true)
{
var jsonText = responseStream.ReadLine();
//Success
int wait = 250;
//Write Status
var status = string.Empty;
var json = new DataContractJsonSerializer(status.GetType());
byte[] byteArray = Encoding.UTF8.GetBytes(jsonText);
var stream = new MemoryStream(byteArray);
status = json.ReadObject(stream) as string;
Console.Beep();
Console.WriteLine(status);
}
}
如果有其他人通过了这个障碍,我真的很感激.我厌倦了诅咒这个.
I would really appreciate to see if anyone else has passed this hurdle. I'm tired of cursing at this.
推荐答案
在计算签名之前,您尚未将 HTTP 方法或基本 URL 包含到 baseString 中.这取自 Twitter 网站:
You haven't included the HTTP method or the base URL into your baseString before you compute the signature. This is taken from the Twitter site:
创建签名基本字符串
到目前为止收集的三个值必须连接成一个字符串,从中生成签名.这被 OAuth 规范称为签名基础字符串.
The three values collected so far must be joined to make a single string, from which the signature will be generated. This is called the signature base string by the OAuth specification.
要将 HTTP 方法、基本 URL 和参数字符串编码为单个字符串:
To encode the HTTP method, base URL, and parameter string into a single string:
将 HTTP 方法转换为大写,并将输出字符串设置为等于该值.
Convert the HTTP Method to uppercase and set the output string equal to this value.
附加&"字符到输出字符串.
Append the '&' character to the output string.
对 URL 进行百分比编码并将其附加到输出字符串中.
Percent encode the URL and append it to the output string.
附加&"字符到输出字符串.
Append the '&' character to the output string.
百分比编码参数字符串并将其附加到输出字符串.
Percent encode the parameter string and append it to the outputstring.
这篇关于尝试调用 Twitter 流 API 时出现 401 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!