Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter: verifying username and password in C#

Bounty Question

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

The remote server returned an error: (401) Unauthorized.

Sample code to verify the UserName and Password will be really appreciated

Bounty Question Ends


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.

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");

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.

How can I validate the username/password without posting anything (in Twitterizer or otherwise)?

like image 326
Roee Adler Avatar asked Jul 31 '09 07:07

Roee Adler


People also ask

How do I get my Twitter verification code?

Tap Account, then tap Security. Tap Login Requests to see a list of all requests. If you're still stuck, you can also request a login code to be sent to your phone via text message. Click the link Request a code sent to your phone via SMS when you log in to your account on twitter.com.

How do I recover my Twitter account without username?

I cannot remember the username I used.Use your account email address and password to log into Twitter. If your password is not working, request a new password by entering your email address in our reset form. If our system cannot find your email address, you may be entering the wrong one.

How can I get my Twitter without a phone number?

Go to the password recovery page on Twitter. Try with the username first. If you enter a valid username, Twitter will prompt you to enter the email address associated with your account. If you provide the valid email, you will get a password reset link by email and you can proceed.


1 Answers

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.

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;
}
like image 141
mkgrunder Avatar answered Sep 20 '22 06:09

mkgrunder