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
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