Post “Hello World” to twitter from .NET application

后端 未结 6 1314
耶瑟儿~
耶瑟儿~ 2020-12-14 04:30

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

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-14 05:14

    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 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.");
                    }
                }
            }
        }
    }
    

提交回复
热议问题