Youtube数据api v3视频上传错误

时间:2017-01-29 08:14:31

标签: c# video youtube-api google-api-dotnet-client youtube-data-api

我试图搜索所有人以找出答案并没有找到任何解决方案。 从youtube data v3将视频上传到youtube频道时,我收到异常。我为已安装的应用程序创建了密钥。甚至视频标题及其详细信息也可以上传。唯一的问题是我得到了异常

任务已取消。有关详细信息,请参阅最后的堆栈跟踪。

这是我的代码

  private async Task Run()
    {
        UserCredential credential;
        using (var stream = new FileStream("client_secret_766914936775-1ak2tk5e3u0krlbuu9rdip1sfa9u8k44.apps.googleusercontent.com.json", FileMode.Open, FileAccess.Read))
        {
            credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                // This OAuth 2.0 access scope allows an application to upload files to the
                // authenticated user's YouTube channel, but doesn't allow other types of access.
                new[] { YouTubeService.Scope.YoutubeUpload },
                "techno.1",
                CancellationToken.None
            );
        }

        YouTubeService youtubeService = new YouTubeService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "techno ",

        });



        var video = new Video();
        video.Snippet = new VideoSnippet();
        video.Snippet.Title = "Default Video Title";
        video.Snippet.Description = "Default Video Description";
        video.Snippet.Tags = new string[] { "tag1", "tag2" };
        video.Snippet.CategoryId = "22"; // See https://developers.google.com/youtube/v3/docs/videoCategories/list
        video.Status = new VideoStatus();
        video.Status.PrivacyStatus = "unlisted"; // or "private" or "public"
        var filePath = @"abc.mp4"; // Replace with path to actual movie file.

        using (var fileStream = new FileStream(filePath, FileMode.Open))
        {
            var videosInsertRequest = youtubeService.Videos.Insert(video, "snippet,status", fileStream, "video/*");
            videosInsertRequest.ProgressChanged += videosInsertRequest_ProgressChanged;
            videosInsertRequest.ResponseReceived += videosInsertRequest_ResponseReceived;

            await videosInsertRequest.UploadAsync();
        }
    }

    private void videosInsertRequest_ProgressChanged(Google.Apis.Upload.IUploadProgress progress)
    {
        switch (progress.Status)
        {
            case UploadStatus.Uploading:
                Console.WriteLine("{0} bytes sent.", progress.BytesSent);
                break;

            case UploadStatus.Failed:
                Console.WriteLine("An error prevented the upload from completing.\n{0}", progress.Exception);
                break;
        }
    }

    private void videosInsertRequest_ResponseReceived(Video video)
    {
        Console.WriteLine("Video id '{0}' was successfully uploaded.", video.Id);
    }
}

我正在获取uploadStatus.Failed videosInsertRequest_ProgressChange方法。

我已经尝试过使用此代码

youtubeService.HttpClient.Timeout = TimeSpan.FromMinutes(60);

但是超时属性不可用。只有它。

  

错误导致上传无法完成。   System.Threading.Tasks.TaskCanceledException:任务被取消   在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)   System.Runtime.CompilerServices.ConfiguredTaskAwaitable 1.ConfiguredTaskAwaiter.GetResult() at Google.Apis.Upload.ResumableUpload 1.d__94.MoveNext()   在   C:\蜂房\ V1.20 \谷歌API-DOTNET客户端的\ src \支持\ GoogleApis \蜜蜂[媒体] \上传\ ResumableUpload.cs:行   652   ---从抛出异常的先前位置开始的堆栈跟踪结束--- at   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()at   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)   System.Runtime.CompilerServices.ConfiguredTaskAwaitable 1.ConfiguredTaskAwaiter.GetResult() at Google.Apis.Upload.ResumableUpload 1.d__91.MoveNext()   在   C:\蜂房\ V1.20 \谷歌API-DOTNET客户端的\ src \支持\ GoogleApis \蜜蜂[媒体] \上传\ ResumableUpload.cs:行   581错误:任务已取消。

2 个答案:

答案 0 :(得分:0)

最后,它起作用了

 youtubeService.HttpClient.Timeout = TimeSpan.FromMinutes(15.00);

我错误地给了TimeSpan。

答案 1 :(得分:0)

超时值十五分钟太长了。我用了一分钟。

youtubeService.HttpClient.Timeout = TimeSpan.FromMinutes(1);

看起来真正的问题是您没有处理返回的错误并继续上传。在https://github.com/google/google-api-dotnet-client-samples查看示例程序“ ResumeableUpload.C​​S.Sample ” 示例程序显示了如何处理在同一实例中发生错误后恢复上载以及如何在重新启动程序时恢复上载。

使用较短的超时时间和恢复上传的代码,在上传服务器忙碌时,您的上传速度会快得多。