我的智慧已经结束了,我已经在这里看了一遍,虽然问了类似我的问题,但我找不到任何答案的确切答案。
我一直试图从C#控制台应用程序中调用https://stream.twitter.com/1.1/statuses/filter.json几天,并继续获得401 Unauthorized错误,无论我做什么都不会消失。适用于任何不流式播放的内容。
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);
}
}
我真的很感激,看看是否还有其他人通过了这个障碍。我厌倦了咒骂。
答案 0 :(得分:3)
在计算签名之前,您没有将HTTP方法或基本URL包含在baseString中。这取自Twitter site:
创建签名基本字符串
到目前为止收集的三个值必须连接起来才能生成一个字符串,从中生成签名。这被OAuth规范称为签名基本字符串。
将HTTP方法,基本URL和参数字符串编码为单个字符串:
将HTTP方法转换为大写,并将输出字符串设置为等于此值。
附加'&'输出字符串的字符。
百分比对URL进行编码并将其附加到输出字符串。
附加'&'输出字符串的字符。
百分比对参数字符串进行编码并将其附加到输出 字符串。