Twitter API to get specific hashtag data using C# ASP.Net

These days, social media specially Twitter rules the internet world. Twitter is one of the most famous social media channel. Daily we are seeing something is trending on twitter, that means a particular term or word is getting high in popularity with a hashtag.

In this blog, we will see how to get tweets with a specific hashtag or analytics of a specific hashtag.

Twitter API to get hashtag data using TweetSharp in Visual Studio 2017

First create an App in Twitter. Go to https://apps.twitter.com/ and click on Create New App.
Make sure to type a unique name for your App.
Once created go to Keys and Access Tokens tab.
Now open Visual Studio 2017
If you want to get tweet records of a specific hashtag then you need to install TweetSharp dll into your solution.
I will show you, how you can use it.
Go to Tools -> NuGet Package Manager, as shown in below screen shot.
Now search for TweetSharp and select your project from right side and click on install.
Once TweetSharp dll added to your solution. You can start writing your code.
Here is the ASPX and C# code.

ASPX Code

<div>
                                Enter a hashtag value – (#)<br />
                                <asp:TextBox ID=”txtHashTag”  runat=”server”></asp:TextBox>
                                <asp:Button ID=”btnTweet” OnClick=”btnTweet_Click” runat=”server” Text=”Submit” />
                            </div>
<asp:Repeater ID=”rptTweets” runat=”server”>
                                <ItemTemplate>
                                    <article>
                                        <div>
                                            <div>
                                                <div>
                                                    <div>
                                                        <span>
                                                            <img src=’<%# Eval(“User.ProfileImageUrl”) %>‘ alt=””></span>
                                                    </div>
                                                    <div><%# Eval(“User.Name”) %></div>
                                                    <div><span></span><%# Eval(“CreatedDate”) %></div>
                                                    
                                                </div>
                                            </div>
                                            <!–Post Content–>
                                            <div>
                                                <div>
                                                    <div>
                                                        <%–Tweet text–%>
                                                        <%# Eval(“Text”) %>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </article>
                                </ItemTemplate>
                            </asp:Repeater>

C# Code

using TweetSharp;
    public static string _ConsumerKey = “”;
    public static string _ConsumerSecret = “”;
    public static string _AccessToken = “”;
    public static string _AccessTokenSecret = “”;
protected void btnTweet_Click(object sender, EventArgs e)
    {
        
        TweetSharp.TwitterService twService = new TweetSharp.TwitterService(_ConsumerKey, _ConsumerSecret);
        twService.AuthenticateWith(_AccessToken, _AccessTokenSecret);
        var twSearch = twService.Search(new TweetSharp.SearchOptions { Q = txtHashTag.Text.Trim(), Resulttype = TweetSharp.TwitterSearchResultType.Recent, Count = 1000 });
        List<TwitterStatus> tweetList = new List<TwitterStatus>(twSearch.Statuses);
        rptTweets.DataSource = tweetList;
        rptTweets.DataBind();
    }

Enter a keyword with # and click on submit button to get the result.

Note – Twitter can give you maximum 100 tweets and default count is 15.

You can change the count in below line –


var twSearch = twService.Search(new TweetSharp.SearchOptions { Q = txtHashTag.Text.Trim(), Resulttype = TweetSharp.TwitterSearchResultType.Recent, Count = 1000 });

Either you can choose Recent Tweets or Popular Tweets or Mixed.

If you will remove the count option then it will return 15 tweets only.

You can keep Count value 100 or 200 0r 1000 but it will return 100 tweets only.
In some cases you may get errors like this-

JSON integer 4665092905 is too large or small for an Int32. Path ‘Data[0].CampaignID’

Value was either too large or too small for an Int32 when processing image

JSON integer is too large or small for an int32 tweetersharp


These errors are coming because TweetSharp dll is not updated as per Twitter API
To overcome these issues, uninstall TweetSharp and install TweetSharp-Unofficial



You may also like below blogs-

Web API interview Questions and Answers
ASP.Net Web API – Media Type Formatter
ASP.Net Web API Security

Leave a Comment

RSS
YouTube
YouTube
Instagram