1

I am currently trying to do a search from Twitter using C#. I am new to C#, and after finding that I should use TweetSharp (https://stackoverflow.com/questions/27266419/twitter-api-1-1-with-net-3-5?noredirect=1&lq=1#=) and installing it with NuGet on VisualStudio, I struggle to make a simple authentification and search, as I did not find any simple example based solely on TweetSharp. I already got my API key, API secret, access token and access token secret (on the Twitter dev website). All I want is to get some JSON data after searching a hashtag.

Simple example code based on uniquely on TweetSharp (and no other libraries, especially ones not findable on NuGet) would save my life ! A documentation for TweetSharp might also do the trick, currently I did not find it...

Cheers, Kevin

Community
  • 1
  • 1
Kevin
  • 33
  • 1
  • 6
  • I might not be objective but why would you want to use a library that has not been maintained for years? – Linvi Jul 21 '16 at 16:11
  • At this point, I could use anything. But I did not find any maintained library... At this point all I want is to have the result from a search on twitter returned in a JSON format... – Kevin Jul 22 '16 at 09:33

1 Answers1

1

Following your first comment. Tweetinvi is a library that has been actively maintained for 4 years now as compared to Tweetsharp. It has been available on nuget since 2013 and has recently published its first major version 1.0.

To answer your question using Tweetinvi:

// Authentication
Auth.SetUserCredentials("CONSUMER_KEY", "CONSUMER_SECRET", "ACCESS_TOKEN", "ACCESS_TOKEN_SECRET");

// Get json directly
var tweetsJson = SearchJson.SearchTweets("hello");

// Get json from ITweet objects
var tweets = Search.SearchTweets("hello");
// JSON Convert from Newtonsoft available with Tweetinvi
var json = JsonConvert.SerializeObject(tweets.Select(x => x.TweetDTO));
var tweetDTOsFromJson = JsonConvert.DeserializeObject<ITweetDTO[]>(json, JsonPropertiesConverterRepository.Converters);
var tweetsFromJson = Tweet.GenerateTweetsFromDTO(tweetDTOsFromJson);
Linvi
  • 2,077
  • 1
  • 14
  • 28
  • 1
    Great it worked fairly smoothly (libraries compatibility issues I had to solve). Thank you very much :) – Kevin Jul 25 '16 at 16:05
  • Great I am glad it helped :) – Linvi Jul 25 '16 at 16:11
  • @Linvi Tweetinvi is definitely a great client library for Twitter. You have done some great work! However, I have multiple concerns regarding thread safety. Setting static variables with credentials makes me very worry that I would be reading/posting data for another user. I would expect to pass around the credentials. Right now it uses static variables and I have little/no guarantee that the data I am getting is for the user I intended to. – Martin Jun 28 '17 at 03:41
  • @Martin As explained on gitter Tweetinvi uses ThreadStatic which result in each thread having their own credentials. – Linvi Jul 01 '17 at 21:21