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