我正在使用Linqtotwitter版本5。以下代码有时有效,有时无效。如果我将断点放在指示的位置并等待一会儿,它几乎总是可以工作。
当它不起作用时,我会收到异常消息“无效的视频”。
我发布了一个示例视频here,但它们符合Twitter发布的here要求。视频大小为75MB,时长为34秒。几乎所有视频的行为都相同。
我已基于示例here
编写了此代码我已将代码取出,并将其放入具有相同行为的控制台应用程序中。
class Program
{
static void Main(string[] args)
{
Task.Run(async () =>
{
UploadMedia theMedia = new UploadMedia();
int theResult = await theMedia.doitAsync();
Console.Write("Return value =" + theResult.ToString());
} ).GetAwaiter().GetResult();
}
}
public class UploadMedia {
public async Task<int> doitAsync()
{
TwitterContext twitterContext = getContext();
List<string> _theMedia = new List<string>();
_theMedia.Add(@"C:\Temp\RecentMedia\MOV_1963.mp4");
try
{
string lsMediaType = "video/mp4";
string lsMediaCat = "tweet_video";
var imageUploadTasks = new List<Task<Media>>();
Task<Media> lsoImageMedia;
foreach (var lsItem in _theMedia)
{
Console.Write("Sending " + lsItem);
lsoImageMedia = twitterContext.UploadMediaAsync(System.IO.File.ReadAllBytes(lsItem), lsMediaType, lsMediaCat);
imageUploadTasks.Add(lsoImageMedia);
};
await Task.WhenAll(imageUploadTasks);
List<ulong> mediaIds =
(from tsk in imageUploadTasks
select tsk.Result.MediaID)
.ToList();
// PUT A BREAKPOINT HERE AND WAIT A WHILE, AND THE VIDEO WILL UPLOAD
Status tweet = await twitterContext.TweetAsync("This is an example video", mediaIds);
if (tweet != null)
{
Console.Write(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Status returned: " + "(" + tweet.StatusID + ")" +
tweet.User.Name + ", " + tweet.Text);
}
return 0;
}
catch (Exception ex)
{
Console.Write("Exception whilst sending/processing media: " + ex.Message + " : " + ex.ToString());
return -1;
}
}
static private TwitterContext getContext()
{
SingleUserAuthorizer authorizer = new SingleUserAuthorizer();
SingleUserInMemoryCredentialStore cred = new SingleUserInMemoryCredentialStore();
cred.ConsumerKey = TwitterConstants.sConsumerKey;
cred.ConsumerSecret = TwitterConstants.sConsumerKeySecret;
cred.AccessToken = TwitterConstants.sAccessToken;
cred.AccessTokenSecret = TwitterConstants.sAccessTokenSecret;
authorizer.CredentialStore = cred;
var twitterContext = new TwitterContext(authorizer);
return twitterContext;
}
}