Twitter:验证用户名和密码在C#
题
赏金问题
我使用的c#3.5形式窗口的应用程序。我使用的代码中提到的接受的答案。和我下错误
远程服务器返回一个错误:(401)未经授权的。
代码样本来验证用户名和密码,将非常感激
赏金问题结束
我有一个应用程序与以下使用情况:当用户首先开始采用的应用程序,他投入自己的用户名和密码。然后,在一个较晚的阶段,应用程序可以更新他的状态。
目前我使用Twitterizer,但我相信这个问题的范围之内的具体的图书馆我使用。以下是两个相关的行代码:
Twitter twitter = new Twitter("username", "password", "source");
twitter.Status.Update("update");
建设Twitter的对象不扔一个异常,如果用户名和密码不正确。这可能是因为什么被送到了这一点。另一方面,将状态更新并扔了一个异常,如果用户名和密码都是无效的。
我的问题是,我想验证用户名/密码点的用户输入的,而不是在试图以后的更新。
怎么我可以验证用户名和密码,没有发布任何东西(在Twitterizer或者)?
解决方案
快速查看peSHIr提到的verify_credentials API,我写了一个小程序,似乎可以解决这个问题。现在已经很晚了,但我能够测试几次并且似乎有效。
在我的函数中,如果我得到一个HttpResponseCode.OK,我只返回true,如果我得到任何其他内容或抛出异常,则返回false。如果twitter不喜欢uid / password,则会抛出401错误(未经授权)。
public bool CheckTwitterCredentials(string UserName, string Password)
{
// Assume failure
bool Result = false;
// A try except block to handle any exceptions
try {
// Encode the user name with password
string UserPass = Convert.ToBase64String(
System.Text.Encoding.UTF8.GetBytes(UserName + ":" + Password));
// Create our HTTP web request object
HttpWebRequest Request =
(HttpWebRequest)WebRequest.Create("http://twitter.com/account/verify_credentials.xml");
// Set up our request flags and submit type
Request.Method = "GET";
Request.ContentType = "application/x-www-form-urlencoded";
// Add the authorization header with the encoded user name and password
Request.Headers.Add("Authorization", "Basic " + UserPass);
// Use an HttpWebResponse object to handle the response from Twitter
HttpWebResponse WebResponse = (HttpWebResponse)Request.GetResponse();
// Success if we get an OK response
Result = WebResponse.StatusCode == HttpStatusCode.OK;
} catch (Exception Ex) {
System.Diagnostics.Debug.WriteLine("Error: " + Ex.Message);
}
// Return success/failure
return Result;
}
其他提示
你可以尝试使用API呼叫 帐户/verify_credentials.我们希望API库使用已经支持这个..Twitter是臭名昭着的现在真的很讨厌的第三方程序,所以除非你有很好的理由做了些什么Twitter,只是远离...
我使用过其他Twitter库,但没有一个支持检查用户名和密码的验证。这可能是因为除非我们尝试执行需要身份验证的操作,否则Twitter API无法验证用户名和密码。
您可以做的一件事是尝试获取朋友列表或任何其他需要身份验证的方法。
Twitter API多年来一直不支持用户名/密码。相反,您有OAuth,它允许用户授权您的应用程序代表他们行事。 Twitter有一个 account / verify_credentials 端点,可用于验证是否您仍然授权您的应用程序的用户。以下是如何使用LINQ to Twitter调用此端点的示例:
var accounts =
from acct in twitterCtx.Account
where acct.Type == AccountType.VerifyCredentials
select acct;
您可以访问 Account / VerifyCredentials 文档了解更多详情:
正如@ joe-mayo所知,你必须切换到OAuth。 twitter已过期其API的v1,他们在以下网址中记录了 https://dev.twitter.com/文档/ FAQ#17750 。
这是我写的一个函数,它将在C#中验证Twitter用户名和密码:
public bool isTwitterValid(string username, string password)
{
try
{
string user = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(username + ":" + password));
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://twitter.com/statuses/verify.xml");
request.Method = "POST";
request.ServicePoint.Expect100Continue = false;
request.Headers.Add("Authorization", "Basic " + user);
request.ContentType = "application/x-www-form-urlencoded";
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string responseString = reader.ReadToEnd();
reader.Close();
}
catch (Exception ex)
{
if (ex.Message.Contains("404")) { return true; }
}
return false;
}