您好我正在创建一个GUI应用程序,用户可以在 twitter 上登录用户ID和密码。 我有以下代码,但它不适合我,错误 403 Forbidden
StringBuilder sb = new StringBuilder();
byte[] buf = new byte[8192];
// encoding username/password
string user = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(txtId.Text + ":" + txtPassword.Text));
//connect with verify page
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://api.twitter.com/1.1/account/verify_credentials.xml");
// HttpWebRequest request =(HttpWebRequest)WebRequest.Create("http://twitter.com/account/verify_credentials.xml");
// setting method to GET- This API expects a GET method Call
request.Method = "POST";
// setting authorization levels
request.Headers.Add("Authorization", "Basic " + user);
request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
//request.ContentType = "application/x-www-form-urlencoded";
// set the response from the GET command
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream resStream = response.GetResponseStream();
// collect info returned from the GET call
string tempStream = null;
int count = 0;
do
{
count= resStream.Read(buf, 0, buf.Length);
if(count!=0)
{
tempStream= Encoding.ASCII.GetString(buf,0,count);
sb.Append(tempStream);
}
}while(count>0);
//convert result to XML DOC
XmlDocument doc = new XmlDocument();
doc.LoadXml(sb.ToString());
// get person name from the newly created xml document
XmlNodeList nodeList = doc.SelectNodes("/user/name");
foreach (XmlNode node in nodeList)
personName = node.InnerText;
label1.Text = "Welcome, " + personName + "!";
答案 0 :(得分:2)
默认情况下,Twitter不再提供用户名/密码访问权限。 要访问此类功能,您必须与一个非常强大的案例(和公司)私下联系,以便他们授权您的应用程序使用它。 例如,授权访问此身份验证的最后一家公司是2年前的Instagram ...
您要使用的是从应用程序和用户信息执行查询的OAuth机制。
我建议您使用我为使用C#(https://github.com/linvi/tweetinvi)的Twitter开发人员开发的库。
您可以使用以下代码行完成您想要的操作:
Auth.SetUserCredentials("CONSUMER_KEY", "CONSUMER_SECRET", "ACCESS_TOKEN", "ACCESS_TOKEN_SECRET");
var authenticatedUser = User.GetAuthenticatedUser();
label1.Text = "Welcome, " + authenticatedUser.Name + "!";