简体   繁体   中英

Post a status update to twitter using c#

Every Example I can find is either very outdated or always comes back 401. Can anyone at all provide a working example of posting a status update to twitter?

Even the below always fails. I get redirected to twitter - great. I can confrim the access codes are correct and match my application, but on acutally posting the update - error is unknown...

What on earth is wrong here? Does matter what app I use or which twitter account.

Using Twitteriser2.dll

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

The TwitterRepsonse object has an "ErrorMessage" property. You should probably start by looking at the information in there to give you some guidance.

Why don't you use Tweetinvi. Tweetinvi will allow you to post in 1 line and get error messages in line too. Here is an example.

TwitterCredentials.SetCredentials("Access_Token", "Access_Token_Secret", "Consumer_Key", "Consumer_Secret");
var tweet = Tweet.PublishTweet("Hello!");

if (tweet == null)
{
    var exceptionDetails = ExceptionHandler.GetLastException().TwitterExceptionInfos.First().Message;
}

You can find the documentation here : https://tweetinvi.codeplex.com/documentation

Also have a look at https://tweetinvi.codeplex.com/discussions/536895 if you are using it with ASP.NET.

Got it working eventually. The fault isn't exactly known as I didn't have to change the code but what I did was re-download Twitterizwer and Built it (Required adding a ref to C++ components for what ever reason) and it then worked so I can only see that it was somehow faulty the first time round.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM