How to use linqtotwitter V3

It's good that I recently upgraded to V3, but it broke a lot of things

How can I fix them?

Number 1:

This no longer works, since definitions like Credentials and InMemoryCredentials

var auth = new SingleUserAuthorizer { Credentials = new InMemoryCredentials { ConsumerKey = srtwitterConsumerKey, ConsumerSecret = srtwitterConsumerSecret, OAuthToken = srtwitterOAuthToken, AccessToken = srtwitterAccessToken } }; 

Number 2: No more definition for GetFileBytes

 var mediaItems = new List<Media> { new Media { Data = Utilities.GetFileBytes(srImageUrl), FileName = srTweet.Split(' ')[0]+".jpg", ContentType = MediaContentType.Jpeg } }; 

Number 3: no definition for TweetWithMedia

 var tweet = twitterContext.TweetWithMedia(srTweet, false, mediaItems); 

Number 4: no definition for UpdateStatus

 var tweet = twitterContext.UpdateStatus(srTweet); 

Number 5: Definition for CreateFavorite

 var vrResult = twitterContext.CreateFavorite(srRetweetId); 

And I can not find a single example for V3

He always says twitterCtx , but how do you get twitterCtx in the first place?

+6
source share
1 answer

LINQ to Twitter v3.0 is asynchronous, which means that the naming conventions have changed, as well as the way some code is called. Some changes related to consistency or improved cross-platform performance. It is also a portable class library (PCL) that allows you to run it on multiple platforms. Here is a summary of some of your questions:

  • Try the following:

      var auth = new SingleUserAuthorizer { CredentialStore = new SingleUserInMemoryCredentialStore { ConsumerKey = ConfigurationManager.AppSettings["consumerKey"], ConsumerSecret = ConfigurationManager.AppSettings["consumerSecret"], AccessToken = ConfigurationManager.AppSettings["accessToken"], AccessTokenSecret = ConfigurationManager.AppSettings["accessTokenSecret"] } }; 
  • The previous GetFileBytes implementation has cross-platform problems, so I removed it. You will need to write your own code to read the bytes of the file. Here's the old implementation:

     /// <summary> /// Reads a file into a byte array /// </summary> /// <param name="filePath">Full path of file to read.</param> /// <returns>Byte array with file contents.</returns> public static byte[] GetFileBytes(string filePath) { byte[] fileBytes = null; using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) using (var memStr = new MemoryStream()) { byte[] buffer = new byte[4096]; memStr.Position = 0; int bytesRead = 0; while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) { memStr.Write(buffer, 0, bytesRead); } memStr.Position = 0; fileBytes = memStr.GetBuffer(); } return fileBytes; } 
  • Here is one of the overloads of TweetWithMediaAsync:

      Status tweet = await twitterCtx.TweetWithMediaAsync( status, PossiblySensitive, Latitude, Longitude, PlaceID, DisplayCoordinates, imageBytes); 
  • Now this is called TweetAsync:

      var tweet = await twitterCtx.TweetAsync(status); 
  • Here is an example of CreateFavoriteAsync:

      var status = await twitterCtx.CreateFavoriteAsync(401033367283453953ul); 
  • You need to create an instance of TwitterContext - here is an example:

      var twitterCtx = new TwitterContext(auth); 

For more information, you can download the source code and view working examples for several technologies. Examples of project names are prefixed by Linq2TwitterDemos_:

https://linqtotwitter.codeplex.com/SourceControl/latest#ReadMe.txt

Each API call is documented, as well as documentation on other aspects of LINQ to Twitter:

https://linqtotwitter.codeplex.com/documentation

+9
source

All Articles