views:

281

answers:

2

Hi I am working on sending twitter updates from my asp.net website. I have the authorization down but I am stuck when it gets to sending the tweet here is my code behind:

  protected void btnAuth_Click(object sender, EventArgs e)
{
    // add these to web.config or your preferred location
    var consumerKey = ConfigurationManager.AppSettings["consumerKey"];
    var consumerSecret = ConfigurationManager.AppSettings["consumerSecret"];

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

        //Step 2: Redirect User to Requested Token
        Response.Redirect("http://twitter.com/oauth/authorize?oauth_token="+ RequestToken.Token);
    }
    else
    {
        //For Valid User
        string Oauth_Token = Request.QueryString["oauth_token"].ToString();

        var accessToken = OAuthUtility.GetAccessToken(consumerKey, consumerSecret, Oauth_Token, txtPIN.Text.Trim());

        lblMessage.Text = "<b>Hello "  + accessToken.ScreenName + ", Welcome to my Twitter App<b>";
        lblMessage.Text += "<br/> Token: " + accessToken.Token;
        lblMessage.Text += "<br/> TokenSecret: " + accessToken.TokenSecret;
        lblMessage.Text += "<br/> UserId: " + accessToken.UserId;
        lblMessage.Text += "<br/> VerificationString: " + accessToken.VerificationString;
    } 
}

 protected void  btnTweet_Click(object sender, EventArgs e)
{
    // add these to web.config or your preferred location
    var consumerKey = ConfigurationManager.AppSettings["consumerKey"];
    var consumerSecret = ConfigurationManager.AppSettings["consumerSecret"];

    OAuthTokens accessToken = new OAuthTokens();
    accessToken.AccessToken = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    accessToken.AccessTokenSecret = "xxxxxxxxxxxxxxxxxxxx";
    accessToken.ConsumerKey = consumerKey;
    accessToken.ConsumerSecret = consumerSecret;

    TwitterStatus TweetStatus = new TwitterStatus();
    TweetStatus.Update(accessTokens, txtTweet.Text);                        
}   

I dont know how to get the AccessToken & AccessTokenSecret. Any help would be great thanks.

+1  A: 

The access token and secret values are returned to your application from Twitter when you call the OAuthUtility.GetAccessToken method:

var accessToken = OAuthUtility.GetAccessToken(consumerKey, consumerSecret, Oauth_Token, Request.QueryString["oauth_verifier"]);

The one-time authorization process goes as follows: 1) Get a request token 2) Send the user to Twitter to login and grant access 3) Receive the user at the callback url, collect the oauth_token and oauth_verifier values from the querystring 4) Exchange the request token and verifier for the access token

After you have the access token, you should store it so that the user isn't required to go through the process again (the access token does not expire).

I noticed that you're collecting a PIN value from the user, but it appears as though your application is a website. The web flow (not pin-based authentication) will provide a much more pleasant user experience.

If you have more questions, please post them to the Twitterizer forums, http://forums.twitterizer.net.

Ricky Smith
A: 

After a successful login, you just have to set all four variables in your OAuthTokens (AccessToken & AccessTokenSecret are both return on successful OAuthUtility.GetAccessToken)

I suggest you store you AccessToken and AccessToken in a Cookie once Authenticated.

Create a Static Class where you can return all four tokens and do a check if all four values are supplied ELSE logged-out.

Marc Uberstein