Twitter:在 C# 中验证用户名和密码 [英] Twitter: verifying username and password in C#
问题描述
悬赏问题
我使用的是 c# 3.5 Window Forms 应用程序.我正在使用接受的答案中提到的代码.我得到以下错误
I am using c# 3.5 Window Forms Application. I am using the code mentioned in the accepted answer. and I am getting below error
验证用户名和密码的示例代码将不胜感激
悬赏问题结束
我有一个具有以下用例的应用程序:当用户第一次开始使用该应用程序时,他输入了他的用户名和密码.然后,在更晚的阶段,应用程序可能会更新其状态.
I have an application with the following use-case: when the user first starts using the application, he inputs his username and password. Then, at a much later stage, the application may update his status.
目前我正在使用 Twitterizer,但我相信这个问题超出了我正在使用的特定库的范围.以下是两行相关的代码:
Currently I'm using Twitterizer, but I believe the question is beyond the scope of the specific library I'm using. Following are the two relevant lines of code:
Twitter twitter = new Twitter("username", "password", "source");
twitter.Status.Update("update");
如果用户名/密码不正确,Twitter 对象的构造不会抛出异常.这可能是因为此时没有发送任何内容.另一方面,如果用户名/密码无效,状态更新会抛出异常.
The construction of the Twitter object does not throw an exception if the username/password are incorrect. This is probably because nothing is sent at this point. On the other hand, the status update does throw an exception if the username/password are invalid.
我的问题是我想在用户输入时验证用户名/密码,而不是在尝试发布更新时.
My problem is that I want to validate the username/password at the point of user input, not when trying to post the update.
如何在不发布任何内容的情况下验证用户名/密码(在 Twitterizer 或其他方式中)?
How can I validate the username/password without posting anything (in Twitterizer or otherwise)?
推荐答案
快速浏览 peSHIr 提到的 verify_credentials API,我编写了一个小程序,它似乎可以解决问题.已经晚了,但我能够对其进行几次测试,并且似乎可以正常工作.
Taking a quick look at the verify_credentials API as mentioned by peSHIr, I wrote a little routine which seems to do the trick. It's late, but I was able to test it a couple of times and seems to work.
在我的函数中,如果我得到一个 HttpResponseCode.OK,我只是返回 true,如果我得到其他任何东西或抛出异常,我就会返回 false.如果 Twitter 不喜欢 uid/密码,则会抛出异常并显示 401 错误(未授权).
In my function, I am just returning true if I I get an HttpResponseCode.OK, and false if I get anything else or an exception is thrown. If twitter does not like the uid/password an exception will be thrown with a 401 error (not authorized.)
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;
}
这篇关于Twitter:在 C# 中验证用户名和密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!