0

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

Wahtever
  • 3,597
  • 10
  • 44
  • 79

1 Answers1

1

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;
 }
user1797792
  • 1,169
  • 10
  • 26
  • it seems to be working fine for posting but when trying to retrieve the time line using the code you provided i get this error: `Unexpected token when deserializing object: StartObject. Path '[1]', line 1, position 1864.`/ – Wahtever Nov 08 '12 at 23:53
  • Twitterizer uses the Json.Net library to parse the Json that Twitter returns. There seems to be a problem that other users are also [experiencing](http://forums.twitterizer.net/viewtopic.php?f=9&t=1096). Do you have the latest version of Twitterizer2? Where did you get it? I've got mine from NuGet and haven't had this error. – user1797792 Nov 09 '12 at 08:25
  • thank you. i uninstalled and reinstalled via nuget and the error seems to be gone but now it throws this error `Object reference not set to an instance of an object.` **at this line** `foreach (var tweet in tweets.ResponseObject)` – Wahtever Nov 09 '12 at 10:48
  • That means you haven't initialized `tweets` yet. This line of code initializes `tweets`: `TwitterResponse tweets = TwitterTimeline.UserTimeline(authorization_tokens);` – user1797792 Nov 09 '12 at 11:06
  • This means that ResponseObject is still null. There were no tweets that could be retrieved. If you set a breakpoint, you can check what's exactly in the tweets object. – user1797792 Nov 09 '12 at 13:29
  • It was an error caused by json.net library so i downgraded to 4.0.5.0 and its working great now, thank you very much – Wahtever Nov 09 '12 at 14:50