YouTube API上传“任务已取消” [英] YouTube API upload "A task was cancelled"

查看:266
本文介绍了YouTube API上传“任务已取消”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经运行了一年多的服务,使用API​​ v3 Upload(使用.NET库Google.Apis.YouTube.v3)将视频上传到YouTube。



在过去的两天里,我们突然开始上传所有上传失败,错误从YouTube返回任务被取消。这些视频部分上传到YouTube上,他们正在经历各种各样的进展(有的只有几MB,有的高达17-20MB,虽然看起来较大的进展在前面的失败列表中)星期五晚上发生了这种情况,星期六上午我能够把所有失败的视频都没有发生事件,但是之后在星期六上午又重新开始了,因为所有的视频星期六下午和晚上失败。我们总共谈了25个左右的视频,所以不是几百个。这些视频大小都在40MB左右。



一次只能上传2-3个视频。在星期六上午我成功推动他们的时候,实际上我一直在推高4-5。

其他人可以提出一个可能的原因,或任何疑难解答提示?我们还没有看到任何服务器连接问题,并且在该服务器上运行的网站没有任何明显的问题。我看不到任何其他的错误,除了这个来自YouTube的任务被取消的错误,直到错误的上传者的成功是成功的。



编辑:我也已经能够重现这个问题,从我的开发环境运行我们的上传之一。我刚刚尝试获取一组全新的API凭据(实际上是针对不同的YouTube帐户),刷新了oAuth令牌并仍然存在相同的问题。

解决方案

您需要在使用服务之前设置超时时间,如果用户更改了服务,则需要暂停所有上传,将服务清空并重新初始化。
$ b

我很确定我可以确认设置YouTubeService.HttpClient.Timeout确实解决了这个问题。



我的延迟。

 内部YouTube服务YouTube服务{get;组; } 

内部异步任务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);
$ b $ try
{

var credentialTask​​ = GoogleWebAuthorizationBroker.AuthorizeAsync(
clientSecrets,scope,Environment.UserName,CancellationToken.None,fileDataStore);

//这是一个黑客,当用户关闭
//浏览器而没有授权时允许进度条超时。这与手头的问题没有关系。
await Task.WhenAny(credentialTask​​,Task.Delay(TimeSpan.FromSeconds(timeoutSeconds)));

if(credentialTask​​.Status == TaskStatus.WaitingForActivation)return;
$ b var initializer = new BaseClientService.Initializer()
{
HttpClientInitializer = credentialTask​​.Result,
ApplicationName = Constants.ApplicationName,
};

YouTubeService =新YouTubeService(初始化程序);

//这是问题的答案。
YouTubeService.HttpClient.Timeout = TimeSpan.FromMinutes(Settings.Default.TimeoutMinutes);

$ b catch(AggregateException)
ExceptionsLogSilent
}

}


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.

解决方案

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);
    }

}

这篇关于YouTube API上传“任务已取消”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆