我正在尝试将 twitter 状态读入我的 Windows 应用程序,该应用程序最初恰好可以工作,但在 twitter 将 OAuth 升级到 1.1 后停止工作。这让我很沮丧,因为我试图让它工作一周,但我无法解决它。现在,我收到错误“找不到远程服务器:410 Gone”。作为参考,这是我的代码:
private void Form1_Load(object sender, EventArgs e)
{
var oauth_consumer_key = "<Consumer Key>";
var oauth_consumer_secret = "<Consumer Secret>";
var oauth_token = "<Access Token>";
var oauth_token_secret = "<Access Token Secret>";
try
{
//oauth implementation details
var oauth_version = "1.0";
var 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();
//messsage api details
//var status = "Updating status via REST API if this works";
var resource_url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
var screen_name = "cornerstorepos";
var count = "2";
//create oauth signature
var baseFormat = "count={7}&oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" +
"&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&screen_name={6}";
var baseString = string.Format(baseFormat,
oauth_consumer_key,
oauth_nonce,
oauth_signature_method,
oauth_timestamp,
oauth_token,
oauth_version,
screen_name,
count
);
baseString = string.Concat("GET&", Uri.EscapeDataString(resource_url),"&", Uri.EscapeDataString(baseString));
var compositeKey = string.Concat(Uri.EscapeDataString(oauth_consumer_secret),"&",Uri.EscapeDataString(oauth_token_secret));
string oauth_signature;
using(HMACSHA1 hasher = new HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey)))
{
oauth_signature = Convert.ToBase64String(hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(baseString)));
}
//create request header
var 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));
//make request
ServicePointManager.Expect100Continue = false;
//var postBody1 = "screen_name=" + Uri.EscapeDataString(screen_name);
//var postBody2 = "count=" + Uri.EscapeDataString(count);
//resource_url += "?" + postBody1 + "&" + postBody2;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resource_url);
request.Headers.Add("Authorization",authHeader);
request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded";
WebResponse response = request.GetResponse();
string responseData = new StreamReader(response.GetResponseStream()).ReadToEnd();
XDocument document = XDocument.Parse(responseData, LoadOptions.None);
var query = from ex in document.Root.Descendants("status")
select new UserStatus
{
Status = HttpUtility.HtmlDecode(ex.Element("text").Value),
};
users = (from u in query.Take(10) where u.Status != "" select u).ToList();
timer.Start();
}
catch
{
return;
}
}
如果有人帮我解决这个错误,那将是很大的帮助。