Twitter: checking username and password in C #

Question about the bounty

I am using Window Forms C # 3.5 application. I am using the code mentioned in the accepted answer. and I get below the error

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

Sample code to validate UserName and Password will be really appreciated

End of voice question


I have an application with the following use case: when a user first starts using the application, he enters his username and password. Then, at a later stage, the application can update its status.

I am currently using Twitterizer, but I believe this question is beyond the scope of the specific library that I am using. The following are two relevant lines of code:

Twitter twitter = new Twitter("username", "password", "source"); 
twitter.Status.Update("update");

Twitter , / . , , . , , / .

, / , .

/, ( Twitterizer )?

+5
6

API- verify_credentials, peSHIr, , , , . , , , .

true, HttpResponseCode.OK false, - . 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;
}
+6

API account/verify_credentials. , API, , . Twitter , , , - Twitter, ...

+1

Twitter, . , , Twitter API , -, .

, , , .

0

Twitter API . OAuth, . account/verify_credentials , , , , . , LINQ to Twitter:

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

Account/VerifyCredentials:

0

@joe-mayo, OAuth. twitter expired v1 API, URL https://dev.twitter.com/docs/faq#17750.

0
source

Here the function I wrote will check twitter username and password in C #:

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

All Articles