Take the 2-minute tour ×

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

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

share|improve this question
Maybe now using another API for Twitter, not Twitterizer. –  Kiquenet Oct 19 at 21:11

6 Answers 6

up vote 6 down vote accepted

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 = 

        // 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;
share|improve this answer
This is not working now. May be Twitter API was updated. And code that you have provided is no more valid. –  Pankaj Oct 8 '13 at 12:28
@abcdefghi API v1 is deprecated. You should use OAuth. In the case you have no choice but to use xAuth (with username and password), you need to request approval. For more info see dev.twitter.com/docs/oauth/xauth –  Jaroslav Jandek Oct 10 '13 at 6:39
@abcdefghi Now in 2014, which is the best practices about it? OAuth sample, Basic not working more I think. –  Kiquenet Oct 19 at 21:14
twitter.com/account/verify_credentials.xml not found. Older version API Twitter –  Kiquenet Oct 20 at 20:35

You could try to use the API call account/verify_credentials. Hopefully the API library you use already supports this.. Twitter is notorious now for really hating third party programmers, so unless you have good reason to do something with Twitter, just stay away...

share|improve this answer
The code is not working in accepted answer. May be Twitter API was updated. And code that accepted answer provided is no more valid. –  Pankaj Oct 8 '13 at 12:29

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.

share|improve this answer

Twitter API hasn't supported username/password in years. Instead, you have OAuth, which lets the user authorize your application to act on their behalf. Twitter has an account/verify_credentials endpoint you can use to verify whether the user who's tokens you have still authorizes your app. Here's an example of how you could call this endpoint with LINQ to Twitter:

        var accounts =
            from acct in twitterCtx.Account
            where acct.Type == AccountType.VerifyCredentials
            select acct;

You can visit Account/VerifyCredentials documentation for more details:

share|improve this answer
@Kiquenet That is a reference to a TwitterContext, as in: var twitterCtx = new TwitterContext(auth); - where auth is an instance implementing IAuthorizer. –  Joe Mayo Oct 20 at 4:02
TwitterContext and/or IAuthorizer are object from Twitterize ? Or your custom classes ? –  Kiquenet Oct 20 at 12:30
They are from LINQ to Twitter: linqtotwitter.codeplex.com –  Joe Mayo Oct 20 at 17:50

as @joe-mayo informed, you have to switch to OAuth. twitter expired v1 of their API and they documented that in following url https://dev.twitter.com/docs/faq#17750.

share|improve this answer

Here's a function i wrote that will verify twitter username and password in C# :

public bool isTwitterValid(string username, string password)
        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);
        WebResponse response = request.GetResponse();
        StreamReader reader = new StreamReader(response.GetResponseStream());
        string responseString = reader.ReadToEnd();
    catch (Exception ex)
        if (ex.Message.Contains("404")) { return true; }
    return false;
share|improve this answer
not new version now in 2014 with latests changes Twitter API ? – –  Kiquenet Nov 1 at 10:19

Your Answer


