Post status message & images on Twitter

Social Media’s growth now a days is truly breath taking and cannot be neglected. Businesses have surely realized their power and accepted that social media plays a major role in marketing, as Social media have the power to reach the peoples at personal level with the target audience on daily basis. As we have accepted the social media have a significant impact on consumers it has become necessary to integrate the business with social media.

Facebook and Twitter plays a vital role in connecting to the peoples more easily as Facebook owns the oldest users on the web whereas Twitter has the wealthiest users as their members.

For the same purpose I will be demonstrating the simple steps to use Twitter and Facebook in your application.

How to post messages and Images on Twitter using ASP.Net C#

For integrating twitter to your application we need to generate the keys & access token to authenticate the application.

Generate keys & access token

1. Sign in at “https://apps.twitter.com/” using your twitter account.

Twitter Apps Sign In
Twitter Apps Sign In

 

2. Create new application.

User Window To Add An App
User Window To Add An App

 

3. Enter the following details and create the application.

App Creation Form
App Creation Form

 

4. Details of application.

App Details
App Details

 

5. Application settings.

App Settings Page
App Settings Page

 

6. View keys.

Generated Keys & Access Token Creation For App
Generated Keys & Access Token Creation For App

 

7. Retrieve access tokens.

Generated Access Token For App
Generated Access Token For App

 

8. Assign permission to the application.

App Permissions
App Permissions

Download the TweetSharp DLL

  • You need to have TweetSharp DLL for posting the tweet on Twitter.
  • Download from NUGet or GitHub.

Code to Post message as a tweet on Twitter

// ————————Set key & token values in the variable.

var oauth_consumer_key = “<Consumer Key from App created>”;

var oauth_consumer_secret = “<Consumer Secrete from App created>”;

var oauth_TokenKey = “<Access Token from App Created>”;

var oauth_TokenSecretKey = “<Access Token Secret from App Created>”;

 

// ————————Create the service variable for posting the tweet

var service = new TweetSharp.TwitterService (oauth_consumer_key, oauth_consumer_secret, oauth_TokenKey, oauth_TokenSecretKey);

 

//———————— Send message as a tweet using SendTweet method

var twitterStatus = service.SendTweet(new SendTweetOptions() { Status = “<Message>” });

 

Code to Post image with message as a tweet on Twitter

// ————————Set key & token values in the variable.

var oauth_consumer_key = “<Consumer Key from App created>”;

var oauth_consumer_secret = “<Consumer Secrete from App created>”;

var oauth_TokenKey = “<Access Token from App Created>”;

var oauth_TokenSecretKey = “<Access Token Secret from App Created>”;

 

// ————————Create the service variable for posting the tweet

var service = new TweetSharp.TwitterService (oauth_consumer_key, oauth_consumer_secret, oauth_TokenKey, oauth_TokenSecretKey);

 

// ————————Read Image from the Image path

var stream = new FileStream(@”Imagepath”, FileMode.Open);

String message = “Message”;

 

// ———————— Post on Twitter

var twitterStatus = service.SendTweetWithMedia(new SendTweetWithMediaOptions(){

Status = message,

Images = new Dictionary<string, Stream> { { “string”, stream } }

});

 

Leave a comment