Home » Send tweet from c#

Send tweet from c#

We can easily tweet manually from our account, but to automate this process we need to write some code, it’s very easy. In this article, we are going to check how we can do this in the C# code

Prerequisite

  1. Basic understanding of C# language
  2. Twitter Developer Account

Here I am Considering everyone is familiar with the C# language, We can check how to create a Twitter Developer account

How to create Twitter Developer Account

  1. Visit Twtter.com and click on Signup button and fill the required details, and Submit
  2. Open Twitter Developer
  3. Visit the above Url and click on Apply button
how to apply

then click on the Apply for a developer account

After that, we need to select the Purpose of the Twitter API.

I am here selecting Hobbyist and making bot options

options selection

After that just fill in the required fields data like Name, Country Name, and Coding experience(If you are developing BOT) Once this step is completed we need to tell Twitter about our project use. This is a very important step, we have to tell you everything about the project, Just add the valid information otherwise Twitter will reject your account request. Just add the required details, accept Policy and Click on Submit.

Once this process is done, one screen will pop up like below

Just click on Create Project button and Add details.

Once the project is created, you will get few details like API key, API secret key, Authentication Token, Consumer Key

This information is very important to trigger tweets from C# or any other language code.

Here we are going to use TweetinviAPI Nuget package to send tweets from code, hit this below command to add the NuGet package.

Install-Package TweetinviAPI -Version 5.0.4

To send tweets from C# code just use the below code snippet

public static async Task AddTweet(string post)
{
try
{
string consumerKey = "sJ1Me2n7RpK1Px3EGLLCV2HtO"; //API Key
string consumerSecret = "1OsRvmejreKDXIlLuIvuAWQuVJGhqhW4IaRJwduaipP6JOjken";  //API Secret Key
string accessToken = "1429476987861487616-M1MjdWRIfQREjsG9w5RfDAwlskOarJ"; // Access Token
string accessSecret = "TqIwVHWYAaJwvENH1wncLQWLK2X54CDbExCwrYHyYaZyP"; //Access Token Secret
var userClient = new TwitterClient(consumerKey, consumerSecret, accessToken, accessSecret);
using (var publishedTweet = userClient.Tweets.PublishTweetAsync(post))
{
}
}
catch (Exception ex)
{
}
}

Just rough code is added, apply logic as per your requirement, above code is enough to send tweets.

Note: Please add your Token and Secret key details.

Need help?

Read this post again, if you have any confusion or else add your questions in Community

Tags: