Thursday 29 December 2011

How to Tweet on user Profile from asp.net website


Before we start  add Your application  to  twitter and get consumer ids

First Download Twitterizer from http://www.twitterizer.net/
and add that dlls to Your Project

Now place Two buttons  say authorization and Tweet buttons  and  text box say tweet text on web form
 Now in Authorization Button click event use this method
  protected void btnauthorize_Click(object sender, EventArgs e)
        {
           


            var consumerKey = System.Configuration.ConfigurationManager.AppSettings["TwitterConsumerKey"];
            var consumerSecret = System.Configuration.ConfigurationManager.AppSettings["TwitterConsumerSecretKey"];
            string callbackadress = "http://" + HttpContext.Current.Request.Url.Host + ":" + HttpContext.Current.Request.Url.Port + HttpContext.Current.Request.Url.PathAndQuery;

            //If User is not valid user
            if (Request.QueryString["oauth_token"] == null)
            {
                //Step 1: Get Request Token
                OAuthTokenResponse RequestToken = OAuthUtility.GetRequestToken(consumerKey, consumerSecret, callbackadress);

                //Step 2: Redirect User to Requested Token
                Response.Redirect("http://twitter.com/oauth/authorize?oauth_token=" + RequestToken.Token);
            }
        
        }
After authenticating that user
 we will be redirected To our url and place this code on page load vent to read user information


 protected void Page_Load(object sender, EventArgs e)
        {
          
            if (!string.IsNullOrEmpty(Request["oauth_verifier"]) && !Page.IsPostBack)
            {
                Readuserinformation();
            }
        }
        public void Readuserinformation()
        {
         
            var consumerKey = ConfigurationManager.AppSettings["TwitterConsumerKey"];
            var consumerSecret = ConfigurationManager.AppSettings["TwitterConsumerSecretKey"];
            string Oauth_Token = Request.QueryString["oauth_token"].ToString();
            var accessToken = OAuthUtility.GetAccessToken(consumerKey,
                                                  consumerSecret,
                                                  Oauth_Token, "");
          //Save this tokens For Tweeting

            string Token: = accessToken.Token;
            string TokenSecrt= accessToken.TokenSecret;
            string userid= accessToken.UserId;

        }
Now in tweetbutton click event call this method


  protected void btnTweet_Click(object sender, EventArgs e)
        {
         
            try
            {

                var consumerKey = ConfigurationManager.AppSettings["TwitterConsumerKey"];
                var consumerSecret = ConfigurationManager.AppSettings["TwitterConsumerSecretKey"];

                OAuthTokens accessToken = new OAuthTokens();
//Use the access token we got  after Reading user information
                accessToken.AccessToken = "xxxxxxx";
                accessToken.AccessTokenSecret = "xxxxxx";
                accessToken.ConsumerKey = consumerKey;
                accessToken.ConsumerSecret = consumerSecret;

                 

                Twitterizer.TwitterStatus.Update(accessToken, tbxTweet.Text);
            }
            catch (Exception ex)
            {


            }


        }
Happy coding

No comments:

Post a Comment