c# – 如何使用linqtotwitter V3

前端之家收集整理的这篇文章主要介绍了c# – 如何使用linqtotwitter V3前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
好吧,我最近升级到V3,但它打破了很多东西

我该如何解决这些问题?

1号 :

这不再适用于Credentials和InMemoryCredentials这样的定义

  1. var auth = new SingleUserAuthorizer
  2. {
  3. Credentials = new InMemoryCredentials
  4. {
  5. ConsumerKey = srtwitterConsumerKey,ConsumerSecret = srtwitterConsumerSecret,OAuthToken = srtwitterOAuthToken,AccessToken = srtwitterAccessToken
  6. }
  7. };

2号:没有GetFileBytes的定义了

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

3号:没有TweetWithMedia的定义

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

4号:没有UpdateStatus的定义

  1. var tweet = twitterContext.UpdateStatus(srTweet);

5号:没有CreateFavorite的定义

  1. var vrResult = twitterContext.CreateFavorite(srRetweetId);

我找不到V3的任何例子

它总是说twitterCtx,但你如何获得twitterCtx?

解决方法

LINQ to Twitter v3.0是异步的,这意味着命名约定已经改变,以及调用某些代码的方式.一些变化是为了保持一致性或改进跨平台操作.它也是一个可移植类库(PCL),允许它在多个平台上运行.以下是您的一些问题的简要说明:

>试试这个:

  1. var auth = new SingleUserAuthorizer
  2. {
  3. CredentialStore = new SingleUserInMemoryCredentialStore
  4. {
  5. ConsumerKey = ConfigurationManager.AppSettings["consumerKey"],ConsumerSecret = ConfigurationManager.AppSettings["consumerSecret"],AccessToken = ConfigurationManager.AppSettings["accessToken"],AccessTokenSecret = ConfigurationManager.AppSettings["accessTokenSecret"]
  6. }
  7. };

>以前的GetFileBytes实现存在跨平台问题,因此我删除了它.您必须编写自己的代码来读取文件的字节.这是旧的实现:

  1. /// <summary>
  2. /// Reads a file into a byte array
  3. /// </summary>
  4. /// <param name="filePath">Full path of file to read.</param>
  5. /// <returns>Byte array with file contents.</returns>
  6. public static byte[] GetFileBytes(string filePath)
  7. {
  8. byte[] fileBytes = null;
  9.  
  10. using (var fileStream = new FileStream(filePath,FileMode.Open,FileAccess.Read))
  11. using (var memStr = new MemoryStream())
  12. {
  13. byte[] buffer = new byte[4096];
  14. memStr.Position = 0;
  15. int bytesRead = 0;
  16.  
  17. while ((bytesRead = fileStream.Read(buffer,buffer.Length)) != 0)
  18. {
  19. memStr.Write(buffer,bytesRead);
  20. }
  21.  
  22. memStr.Position = 0;
  23. fileBytes = memStr.GetBuffer();
  24. }
  25.  
  26. return fileBytes;
  27. }

>这是TweetWithMediaAsync重载之一:

  1. Status tweet = await twitterCtx.TweetWithMediaAsync(
  2. status,PossiblySensitive,Latitude,Longitude,PlaceID,DisplayCoordinates,imageBytes);

>现在称为TweetAsync:

  1. var tweet = await twitterCtx.TweetAsync(status);

>以下是CreateFavoriteAsync的示例:

  1. var status = await twitterCtx.CreateFavoriteAsync(401033367283453953ul);

>你必须实例化TwitterContext – 这是一个例子:

  1. var twitterCtx = new TwitterContext(auth);

有关更多信息,您可以下载源代码并查看多种技术的工作示例.示例项目名称具有Linq2TwitterDemos_前缀:

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

记录每个API调用,以及LINQ到Twitter的其他方面的文档:

https://linqtotwitter.codeplex.com/documentation

猜你在找的C#相关文章