tags:

views:

962

answers:

4

Hello,

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)?

Thanks

A: 

I have used other Twitter Libraries but none of them support checking the username and password for validitity. This might be because Twitter API does not have the facility to validate the username and password unless we try to do something which requires authentication.

One thing you can do is try to get friend list or any other methods that requires authentication.

Shoban
A: 

You could try to use the API call account/verify_credentials. Hopefully the API library you use already supports this, as I think the API is relatively new. Otherwise it would be easy to write yourself: one HTTP GET request should be enough.

peSHIr
+4  A: 

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;
}

I hope this is what you're looking for!

Regards - Mike

mkgrunder
Works great, thanks!
Roee Adler
A: 

from c# how can i reterive latest Tweets from Twitter account("username", "password")

mala
Rise the new question.
Mohanavel