Search code examples
c#asp.nettwittertwitterizer

how to get user timeline using twitterizer2


I am using twitterizer2 to try and get a users recent few tweets and then write them to a table using oAuth not the users screen name, but i am new to twitterizer so i don't know how to do this.

        var step = Request.QueryString["p"].ToString();
        var key = ConfigurationManager.AppSettings["twitterKey"];
        var secret = ConfigurationManager.AppSettings["twitterSecret"];

        if (step == "1")
        {
            var reqquest = OAuthUtility.GetRequestToken(key, secret, "http://localhost:2480/Site/Default2.aspx?p=2").Token;
            Response.Redirect(OAuthUtility.BuildAuthorizationUri(reqquest, true).AbsoluteUri);
        }

        else if (step == "2")
        {
            var token = Request.QueryString["oauth_token"];
            var verifier = Request.QueryString["oauth_verifier"];

            OAuthTokenResponse getTokens = OAuthUtility.GetAccessToken(key, secret, token, verifier);

            var oAuth_token = new OAuthTokens();
            oAuth_token.AccessToken = getTokens.Token;
            oAuth_token.ConsumerKey = key;
            oAuth_token.ConsumerSecret = secret;
            oAuth_token.AccessTokenSecret = getTokens.TokenSecret;

            TwitterResponse<TwitterStatusCollection> tweets = TwitterTimeline.UserTimeline(oAuth_token);

            foreach (var tweet in tweets.ResponseObject)
            {
                Response.Write(tweet.Text);
            }

            //TwitterStatus.Update(oAuth_token, "hello");
        }

how can i do this ? thanks


Solution

  • You should first get the oAuth tokens of the user.

    First of you'll need the user's accesstokens. You can retrieve these with

    var requestToken = OAuthUtility.GetRequestToken("consumerKey", "consumerSecret", "your return url").Token;
    Response.Redirect(OAuthUtility.BuildAuthorizationUri(requestToken, true).AbsoluteUri);
    

    This should ask you to sign in to Twitter. Than you'll return to the return url you've set, with oauth_token and oauth_verifier in the QueryString. These are the accesstokens.

    Now you can use these tokens to retrieve the tweets of this user by using

     TwitterResponse<TwitterStatusCollection> tweets = TwitterTimeline.UserTimeline(authorization_tokens);
    

    Iterate through these by doing something like this

     foreach (var tweet in tweets.ResponseObject)
     {
          string tweetText = tweet.Text;
     }