简体   繁体   中英

Verify Twitter User and Post tweet on his on account C#

I have single asp.net c# page with three text boxes and a button to post the tweet.

1) Input user name

2) Input password

3) Tweet text

4) Post tweet button

Problem is that I want to post the tweet on user account. On my webpage first user will enter his user name and password. I will verify the user information from twitter and then post the tweet on his own account.

Something like this which we can do in Twitterizer.

Twitter twitter = new Twitter("username", "password", "source");

twitter.Status.Update("update");

I know that now we cannot use Twitterizer.

I have used the example mentioned on the following link but it is also not working.

Twitter: verifying username and password in C#

Sample code ///////////////////////////////////////////////////////////////////////////////////////////////////

public bool CheckTwitterCredentials(string UserName, string Password)
{
    // Assume failure
    bool Result = false;

    // A try except block to handle any exceptions
    try {
        // Encode the user name with password
        string UserPass = Convert.ToBase64String(
            System.Text.Encoding.UTF8.GetBytes(UserName + ":" + Password));

        // Create our HTTP web request object
        HttpWebRequest Request = 
            (HttpWebRequest)WebRequest.Create("http://twitter.com/account/verify_credentials.xml");

        // Set up our request flags and submit type
        Request.Method = "GET";
        Request.ContentType = "application/x-www-form-urlencoded";

        // Add the authorization header with the encoded user name and password
        Request.Headers.Add("Authorization", "Basic " + UserPass);

        // Use an HttpWebResponse object to handle the response from Twitter
        HttpWebResponse WebResponse = (HttpWebResponse)Request.GetResponse();

        // Success if we get an OK response
        Result = WebResponse.StatusCode == HttpStatusCode.OK;
    } catch (Exception Ex) {
        System.Diagnostics.Debug.WriteLine("Error: " + Ex.Message);
    }

    // Return success/failure
    return Result;
}

///////////////////////////////////////////////////////////////////////////////////////////////

Now twitter has provided some libraries to work with dot net C#.

https://dev.twitter.com/resources/twitter-libraries#dotnet

.NET

• LINQ2Twitter by @joemayo (examples)

• Spring.NET Social extension for Twitter by SpringSource — A Spring.NET Social extension with connection support and an API binding for Twitter.

• TweetSharp by @danielcrenna — A .net library for Twitter API access

• Tweetinvi maintained by Linvi — a Twitter .Net C# API which has for mission to simplify the development of application for Twitter in C#. The streaming API has been used on research projects and collected around 3.2 million Tweets a day. The Twitter API has been created to be easy to implement new functionality and currently provide access to most of the functionalities. (documentation)

• Crafted.Twitter by @martbrow — A caching compatible solution - with implementations for both ASP.Net Web Forms and MVC. Making it easy to include Tweets in your website.

I have used the following library TweetinviAPI. Mentioned on following link.

https://github.com/linvi/tweetinvi/wiki/Introduction#hello-world

// Set up your credentials ( https://apps.twitter.com )

Auth.SetUserCredentials("CONSUMER_KEY", "CONSUMER_SECRET", "ACCESS_TOKEN", "ACCESS_TOKEN_SECRET");

// Publish the Tweet "Hello World" on your Timeline
Tweet.PublishTweet("Hello World!");

But problem is that it is posting on my own created twitter page not on other user twitter page.

What I want is that by using the above updated libraries is there any way that User put his user name and password on my page and some detail of tweet and post a tweet on his own account.

Please advise me is this possible now or not.

Thanks

Syyed

I am the developer of Tweetinvi.

The username/password authentication is no longer available publicly for Twitter applications. Instead Twitter now requests the developers to use OAuth.

What this mean for you is that in order for a use to authenticate on your app they will have to accept your application from the twitter.com website.

To do so the Twitter REST API provides 2 authentication process. PIN Based and URL Redirect. The URL Redirect is recommended for websites.

You can learn more about it here : https://github.com/linvi/tweetinvi/wiki/Authentication#url-redirect-authentication

I would also suggest you take a look at https://github.com/linvi/tweetinvi/wiki/Authentication#web-application-considerations so that you have a better understanding of the authentication mechanism.

Finally I would suggest that you take a look into Examplinvi.ASP.NET or Examplinvi.ASP.NET.Core which is a MVC project doing exactly what you are trying to do : Authentication + Post Tweet .

If you have more question after that I will be happy to help more!

Cheers, Linvi

Try that code:

var tweet = Search.SearchTweets("stackoverflow");
var textToPublish = $"@{tweet.CreatedBy.ScreenName}";
var reply = Tweet.PublishTweet(new PublishTweetParameters(textToPublish)
                {
                    InReplyToTweet = tweet
                });

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