The sample code in this article demonstrates share twitter post programmatically. To create a twitter application using Twitter apps and get a customer key (API Key) and customer secret (API key). Inorder to share twitter post we need to Install-Package TweetSharp using nuget package manager console and reference to the page.
if you want to share image via twitter refer this link
Import following namespaces,
using System.IO;
using TweetSharp;
Step 1: Tools ->nuget package manager->package manager console->
PM> Install-Package TweetSharp
Step 2: Right click on the "Controllers" folder and add "Twitter" controller. Copy and paste the following code. Please make sure to include "MVC_tutorials.Models" namespace.
private void sharet_witter()
{
var oauth_consumer_key = "your API key";
var oauth_consumer_secret = "Your API secret key";
string token = "access token";
string tokenSecret = "access token secret ";
StringBuilder str = new StringBuilder();
str.AppendLine("hello word");
var service = new TweetSharp.TwitterService(oauth_consumer_key, oauth_consumer_secret);
service.AuthenticateWith(token, tokenSecret);
service.SendTweet(new SendTweetOptions
{
Status = str.ToString()
});
TempData["SuccessMessage"] = "tweeted success";
}
Step 3: Right click on the "Index" action method in the "TwitterController" and add "Index" view. Copy and paste the following code.
<style type="text/css">
.btn {
font-size: 19px;
width: 100px;
height: 45px;
background: #00BCD4;
border-style: solid;
border-color: white;
color: white;
}
</style>
@if (TempData["SuccessMessage"] != null)
{
<div class="errorBlock" style="display: block; padding-left: 65px; color: black; border-color: #f0c36d; background-color: #f9edbe; font-size: 16px">
@TempData["SuccessMessage"]
</div>
}
@Html.ActionLink("share twitter", "share_twitter", new { Class = "btn"})
Output:
Post your comments / questions
Recent Article
- ModuleNotFounEerror:No module named celery in Django Project
- How to get domain name information from a Domain using Python
- ModulenotFoundError: no module named 'debug_toolbar' -SOLUTION
- How to create superuser in django project hosted in cPanel without terminal
- CSS & images not loading in django admin | cpanel without terminal
- Could not build wheels for mysqlclient, which is required to install pyproject.toml-based projects
- How to sell domain name on Godaddy (2023)
- TemplateSyntaxError at / Could not parse the remainder: ' + 1' from 'forloop.counter0 + 1'
Related Article