Search code examples
c#asp.nettwittertwitterizer

how to update twitter status from my asp.net web page


I have created an asp.net web page and I am using twitterizer.framework.dll.

Below is my c# code.

    protected void btnTwitt_Click(object sender, EventArgs e)
    {

        //OAuthTokens tokens = new OAuthTokens();
        //tokens.AccessToken = "--REDACTED--";
        //tokens.AccessTokenSecret = "--REDACTED--";
        //tokens.ConsumerKey = "--REDACTED--";
        //tokens.ConsumerSecret = "--REDACTED--";

        /////

        //TwitterResponse<TwitterUser> showUserResponse = TwitterUser.Show(tokens, "twit_er_izer");

        string TwitterUserName = "My twitter username";
        string TwitterPassword = "my password";
       // string TwitterMessage = txtShout.Text;

        if (TwitterUserName != string.Empty && TwitterPassword != string.Empty)
        {
            try
            {
                //Twitter
               // TwitterAccount twitter = new TwitterAccount(TwitterUserName, TwitterPassword);
                //Twitter tw
                Twitter twitter = new Twitter(TwitterUserName, TwitterPassword);

                string TwitterMsg = txtShout.Text;
                if (txtShout.Text.Length > 120)
                {
                    TwitterMsg = TwitterMsg.Substring(0, 130) + "... For more update logon to DailyFreeCode.com";
                }
                else
                {
                    TwitterMsg = TwitterMsg + "... For more update logon to DailyFreeCode.com";
                }
                twitter.Status.Update(TwitterMsg);
                //Twitterizer.TwitterStatus.Update(tokens,TwitterMsg);


                lblTwitMsg.Text = "Your have shout successfully on http://twitter.com/" + TwitterUserName;

            }
            catch (Exception ex)
            {
                Response.Write("<b>" + ex.Message + "</b><br>" + ex.StackTrace);
            }
        }
    }

Now, I am getting error authorization failed. On this line

twitter.Status.Update(TwitterMsg);

Plz help me, thanks!


Solution

  • If you use the twitterizer framework you shouldn't be passing the username/password. You need to fill:

    OAuthTokens tokens = new OAuthTokens();
    tokens.AccessToken = "XXX";
    tokens.AccessTokenSecret = "XXX";
    tokens.ConsumerKey = "XXX";
    tokens.ConsumerSecret = "XXX";
    

    you can do this by using the following three methods from the framework:

    1:

    public static Uri BuildAuthorizationUri(
        string requestToken
    )
    

    2:

    public static OAuthTokenResponse GetRequestToken(
        string consumerKey,
        string consumerSecret,
        string callbackAddress
    )
    

    3:

    OAuthTokenResponse accessToken = OAuthUtility.GetAccessToken(consumerKey, consumerSecret, requestToken, verifier);
    

    look it up here