TwitterHandler

The TwitterHandler is a full Twitter SDK using DalSoft.RestClient (that even supports uploads).

It works as you would expect, provide your Twitter creds and just call Twitter’s API as you would any other API with DalSoft.RestClient, see examples below.

dynamic restClient = new RestClient("https://api.twitter.com/1.1", new Config
(
	new TwitterHandler
	(
		consumerKey: "your-key", 
		consumerKeySecret: "your-secret", 
		accessToken: "your-token", 
		accessTokenSecret: "your-secret"
	)
));

var result = await restClient.Search.Tweets.Query(new { q = "Hello World" }).Get();

// OR
var result = await restClient.Search.Tweets.Json.Query(new { q = "Hello World" }).Get();

Notice you can append .Json this is so you can paste the API URL directly from Twitter’s documentation, then just add your verb and params and your good to go.

Timeline

dynamic restClient = new RestClient("https://api.twitter.com/1.1", new Config
(
	new TwitterHandler
	(
		consumerKey: "your-key", 
		consumerKeySecret: "your-secret", 
		accessToken: "your-token", 
		accessTokenSecret: "your-secret"
	)
));

var result = await restClient.Statuses.Home_Timeline.Get();

Post Status Update

dynamic restClient = new RestClient("https://api.twitter.com/1.1", new Config
(
	new TwitterHandler
	(
		consumerKey: "your-key", 
		consumerKeySecret: "your-secret", 
		accessToken: "your-token", 
		accessTokenSecret: "your-secret"
	)
));

var result = await restClient.Statuses.Update.Post( new 
{ 
	status = "Posted using DalSoft.RestClient", 
	trim_user = "1" 
});

Post Status Update With Upload

You can upload either a Stream or byte[], the handler will deal with it.

dynamic restClient = new RestClient("https://api.twitter.com/1.1", new Config
(
	new TwitterHandler
	(
		consumerKey: "your-key", 
		consumerKeySecret: "your-secret", 
		accessToken: "your-token", 
		accessTokenSecret: "your-secret"
	)
));

Stream stream = new FileStream("c:\DalSoft1.jpg", FileMode.Open, FileAccess.Read);

var mediaUploadResult =  await restClient.Media.Upload.Post( new { media = stream } );
var statusUpdateResult = await restClient.Statuses.Update.Post( new { status = "Upload" , trim_user = "1", media_ids = mediaUploadResult.media_id } );

Notice the use of https://api.twitter.com/1.1 rather than https://upload.twitter.com/1.1 the handler detects an upload and switches API’s automatically - this means you don’t need to create two instances to upload.

Credits

Credit and thanks to:

https://blog.dantup.com/2016/07/simplest-csharp-code-to-post-a-tweet-using-oauth/

https://retifrav.github.io/blog/2017/11/24/csharp-dotnet-core-publish-twitter/