Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post "Hello World" to twitter from .NET application

Tags:

c#

twitter

My client would like me to use .NET to post to Twitter, and suggests that I use C#.

Q: How do I post "Hello World" to twitter using C#?

This post mentions a library called twitterizer. Isn't there a native way to do it without using a 3rd party library? (Maybe not since authentication is one of the requirements).

like image 565
Phillip Senn Avatar asked May 17 '10 13:05

Phillip Senn


2 Answers

Just use this implemented wrapper for the Twitter API:

https://github.com/danielcrenna/tweetsharp

    var twitter = FluentTwitter.CreateRequest()   
    .AuthenticateAs("USERNAME", "PASSWORD")   
    .Statuses().Update("Hello World!")   
    .AsJson();   

    var response = twitter.Request();  

From: http://code-inside.de/blog-in/2009/04/23/howto-tweet-with-c/

There is even a DotNetRocks interview with the developers.

like image 130
Achilles Avatar answered Sep 27 '22 23:09

Achilles


I created a video tutorial showing exactly how to setup the application inside twitter, install an API Library using nuget, accept an access token from a user and post on that user's behalf:

Video: http://www.youtube.com/watch?v=TGEA1sgMMqU

Tutorial: http://www.markhagan.me/Samples/Grant-Access-And-Tweet-As-Twitter-User-ASPNet

In case you don't want to leave this page:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Twitterizer;

namespace PostFansTwitter
{
    public partial class twconnect : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var oauth_consumer_key = "gjxG99ZA5jmJoB3FeXWJZA";
            var oauth_consumer_secret = "rsAAtEhVRrXUTNcwEecXqPyDHaOR4KjOuMkpb8g";

            if (Request["oauth_token"] == null)
            {
                OAuthTokenResponse reqToken = OAuthUtility.GetRequestToken(
                    oauth_consumer_key,
                    oauth_consumer_secret,
                    Request.Url.AbsoluteUri);

                Response.Redirect(string.Format("http://twitter.com/oauth/authorize?oauth_token={0}",
                    reqToken.Token));
            }
            else
            {
                string requestToken = Request["oauth_token"].ToString();
                string pin = Request["oauth_verifier"].ToString();

                var tokens = OAuthUtility.GetAccessToken(
                    oauth_consumer_key,
                    oauth_consumer_secret,
                    requestToken,
                    pin);

                OAuthTokens accesstoken = new OAuthTokens()
                {
                    AccessToken = tokens.Token,
                    AccessTokenSecret = tokens.TokenSecret,
                    ConsumerKey = oauth_consumer_key,
                    ConsumerSecret = oauth_consumer_secret
                };

                TwitterResponse<TwitterStatus> response = TwitterStatus.Update(
                    accesstoken,
                    "Testing!! It works (hopefully).");

                if (response.Result == RequestResult.Success)
                {
                    Response.Write("we did it!");
                }
                else
                {
                    Response.Write("it's all bad.");
                }
            }
        }
    }
}
like image 42
Mark Hagan Avatar answered Sep 28 '22 01:09

Mark Hagan