我試圖將twitter狀態讀入我的Windows應用程序,該應用程序剛開始工作,但在將升級版OAuth升級到1.1後停止工作。這令我很沮喪,因爲我試圖讓它工作一個星期,但我一直無法解決它。現在,我收到錯誤「找不到遠程服務器:410 Gone」。作爲參考,這裏是我的代碼:Twitter API 1.1 OAuth狀態用戶時間
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;
}
}
這將是巨大的幫助,如果任何人能幫助我解決這個錯誤。