c# – 如何使用linqtotwitter V3

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

我该如何解决这些问题?

1号 :

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

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

2号:没有GetFileBytes的定义了

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

3号:没有TweetWithMedia的定义

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

4号:没有UpdateStatus的定义

var tweet = twitterContext.UpdateStatus(srTweet);

5号:没有CreateFavorite的定义

var vrResult = twitterContext.CreateFavorite(srRetweetId);

我找不到V3的任何例子

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

解决方法

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

>试试这个:

var auth = new SingleUserAuthorizer
    {
        CredentialStore = new SingleUserInMemoryCredentialStore
        {
            ConsumerKey = ConfigurationManager.AppSettings["consumerKey"],ConsumerSecret = ConfigurationManager.AppSettings["consumerSecret"],AccessToken = ConfigurationManager.AppSettings["accessToken"],AccessTokenSecret = ConfigurationManager.AppSettings["accessTokenSecret"]
        }
    };

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

/// <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,buffer.Length)) != 0)
        {
            memStr.Write(buffer,bytesRead);
        }

        memStr.Position = 0;
        fileBytes = memStr.GetBuffer();
    }

    return fileBytes;
}

>这是TweetWithMediaAsync重载之一:

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

>现在称为TweetAsync:

var tweet = await twitterCtx.TweetAsync(status);

>以下是CreateFavoriteAsync的示例:

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

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

var twitterCtx = new TwitterContext(auth);

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

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

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

https://linqtotwitter.codeplex.com/documentation

猜你在找的C#相关文章