Search code examples
c#linq-to-twitter

How to use linqtotwitter V3


Alright I recently upgraded to V3 but it broken many things

How can I fix these ?

Number 1 :

This is not working anymore no such definition as Credentials and InMemoryCredentials

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

Number 2 : no definition for GetFileBytes anymore

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 : no definition for CreateFavorite

var vrResult = twitterContext.CreateFavorite(srRetweetId);

And i can not find any example for V3

It always say twitterCtx but how do you get twitterCtx in the first place ?


Solution

  • LINQ to Twitter v3.0 is async, which means that naming conventions have changed as well as the way to call some code. Some changes were for consistency or improving cross-platform operation. It's also a Portable Class Library (PCL), which allows it to run on multiple platforms. Here's a quick rundown on some of your questions:

    1. Try this:

          var auth = new SingleUserAuthorizer
          {
              CredentialStore = new SingleUserInMemoryCredentialStore
              {
                  ConsumerKey = ConfigurationManager.AppSettings["consumerKey"],
                  ConsumerSecret = ConfigurationManager.AppSettings["consumerSecret"],
                  AccessToken = ConfigurationManager.AppSettings["accessToken"],
                  AccessTokenSecret = ConfigurationManager.AppSettings["accessTokenSecret"]
              }
          };
      
    2. The previous implementation of GetFileBytes has cross-platform issues, so I removed it. You'll have 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;
      } 
      
    3. Here's one of the TweetWithMediaAsync overloads:

          Status tweet = await twitterCtx.TweetWithMediaAsync(
              status, PossiblySensitive, Latitude, Longitude,
              PlaceID, DisplayCoordinates, imageBytes);
      
    4. That is now called TweetAsync:

              var tweet = await twitterCtx.TweetAsync(status);
      
    5. Here's an example of CreateFavoriteAsync:

          var status = await twitterCtx.CreateFavoriteAsync(401033367283453953ul);
      
    6. You have to instantiate TwitterContext - here's an example:

          var twitterCtx = new TwitterContext(auth);
      

    For more information, you can download the source code and see working examples for multiple technologies. The sample project names have the Linq2TwitterDemos_ prefix:

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

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

    https://linqtotwitter.codeplex.com/documentation