问题
We have been running a service for over a year now that uploads videos to YouTube using the API v3 Upload (using the .NET library Google.Apis.YouTube.v3).
In the last two days we have suddenly started having all uploads fail part way through upload, with the error being returned from YouTube "A task was cancelled". The videos are partially uploaded on YouTube, they are getting through varied progress (some only a couple of MB, some as much as 17-20MB, though it does appear that the larger progress ones are in the earlier list of failures).
This happened first on Friday night and I was able to push all the failed videos on Saturday morning without incident, but then later on Saturday morning it started again, and since hen all videos on Saturday afternoon and evening failed. We're talking about a total of 25 or so videos, so not hundreds. The videos are all around 40MB in size.
Only 2-3 videos would usually be uploading at once. While I was successfully pushing them all up on Saturday morning I was actually pushing up about 4-5 at a time.
Can anyone else suggest a possible cause, or any troubleshooting tips? We haven't seen any server connectivity issues otherwise, and the website that runs on this server hasn't had any apparent issues. I can't see any other error coming back other than this "A task was cancelled" error from YouTube, and up until that error the progress from the uploader is success.
Edit: I've also been able to reproduce this issue running one of our uploads from my dev environment. I've just tried getting a whole new set of API credentials (which are actually for a different YouTube account), refreshed the oAuth token and still having the same issue.
回答1:
You need to set the timeout before you use the service and, if the user changes it, you need to pause all uploads, null the service and initialise it again.
I'm pretty sure I can confirm setting the YouTubeService.HttpClient.Timeout does solve the question.
Apologies for my delay to respond.
internal YouTubeService YouTubeService { get; set; }
internal async Task InitialiseYouTubeServiceAsync(int timeoutSeconds)
{
var scope = new List<string> { YouTubeService.Scope.YoutubeUpload, YouTubeService.Scope.Youtube, };
var clientSecrets = new ClientSecrets()
{
ClientId = Constants.ClientId,
ClientSecret = Constants.ClientSecret
};
var fileDataStore = new FileDataStore(Constants.FileDataStore);
try
{
var credentialTask = GoogleWebAuthorizationBroker.AuthorizeAsync(
clientSecrets, scope, Environment.UserName, CancellationToken.None, fileDataStore);
//This is a hack to allow a progress bar timeout when the user closes the
//browser without authorising. It's not associated with the question at hand.
await Task.WhenAny(credentialTask, Task.Delay(TimeSpan.FromSeconds(timeoutSeconds)));
if (credentialTask.Status == TaskStatus.WaitingForActivation) return;
var initializer = new BaseClientService.Initializer()
{
HttpClientInitializer = credentialTask.Result,
ApplicationName = Constants.ApplicationName,
};
YouTubeService = new YouTubeService(initializer);
//This is the answer to the question at hand.
YouTubeService.HttpClient.Timeout = TimeSpan.FromMinutes(Settings.Default.TimeoutMinutes);
}
catch (AggregateException)
{
Exceptions.LogSilent(new Exception(ResourceExceptions.AuthorisationDenied), MethodBase.GetCurrentMethod(), EventLogEntryType.Information);
}
}
来源:https://stackoverflow.com/questions/34249180/youtube-api-upload-a-task-was-cancelled