我正在尝试将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;
}
}
如果有人帮我解决这个错误,那将会很有帮助。
答案 0 :(得分:0)
410 Gone旨在作为对属于Twitter API v1.0的端点的响应。但是,您的resource_url似乎是正确的。您可能希望使用Fiddler来监视您的HTTP流,以查看代码的任何其他部分是否向不推荐的端点发出请求。
答案 1 :(得分:0)
这是你需要做的:
Authenticate and request a user's timeline with Twitter API 1.1 oAuth
我已经为此创建了一个GitHub项目,其中包含以下工作示例: