0
我正在研究WCF REST API集成,也是我第一次與Twitter API合作。我在控制檯應用程序中編寫這些行。請從這裏找到幫助文檔Twitter DocWCF REST for Twitter授權
HttpClient http = new HttpClient("http://twitter.com/statuses/");
http.TransportSettings.Credentials = new NetworkCredential(username, password);
HttpResponseMessage resp = null;
System.Net.ServicePointManager.Expect100Continue = false;
Console.WriteLine("\nPlease enter a command: ");
string command = Console.ReadLine();
while (!command.Equals("q"))
{
try
{
switch (command)
{
case "ls public":
GetStatuses(http, "public_timeline.xml");
break;
case "ls friends":
GetStatuses(http, "friends_timeline.xml");
break;
case "ls":
GetStatuses(http, "user_timeline.xml");
break;
}
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(ex.Message);
Console.ForegroundColor = ConsoleColor.Yellow;
}
Console.WriteLine("\nPlease enter a command: ");
Console.ReadLine();
}
這裏是其他代碼,
static void GetStatuses(HttpClient http, string uri)
{
HttpResponseMessage resp= http.Get(uri);
resp.EnsureStatusIsSuccessful();
DisplayTwitterStatuses(resp.Content.ReadAsXElement());
}
static void DisplayTwitterStatuses(XElement root)
{
var statuses = root.Descendants("status");
foreach (XElement status in statuses)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(status.Element("user").Element("screen_name").Value);
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(" {0} ",status.Element("id").Value);
Console.ForegroundColor = ConsoleColor.White;
string text = status.Element("text").Value;
if (text.Length > 50)
text = text.Remove(50) + "....";
Console.WriteLine(text);
Console.ForegroundColor = ConsoleColor.Yellow;
}
}
如果我選擇「LS公衆」它顯示公衆XML DATAM,但如果我選擇「LS朋友」或「 ls「,即使我的憑證有效,也會拋出授權錯誤。
Unauthorized (401) is not one of the following: OK (200), Created (201), Accepted (202), NonAuthoritativeInformation (203), NoContent (204), ResetContent (205), PartialContent (206)
請幫我找出解決方案!
你有沒有找到一個解決辦法? 我也有這個具體問題。 我覺得我們需要通過trow頭access_key基於Oauth認證過程 – Silagy
感謝silagy,但我仍然面臨這個問題。 – Sujit