【问题标题】:Youtube data api v3 Video Upload ErrorYoutube 数据 api v3 视频上传错误
【发布时间】:2017-06-14 14:32:57
【问题描述】:

我已经尝试搜索所有以找出答案,但没有找到任何解决方案。 从 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.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult() at Google.Apis.Upload.ResumableUpload1.d__94.MoveNext() 在 C:\Apiary\v1.20\google-api-dotnet-client\Src\Support\GoogleApis\Apis[媒体]\Upload\ResumableUpload.cs:line 652 --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult() at Google.Apis.Upload.ResumableUpload1.d__91.MoveNext() 在 C:\Apiary\v1.20\google-api-dotnet-client\Src\Support\GoogleApis\Apis[媒体]\Upload\ResumableUpload.cs:line 581 错误:任务已取消。

【问题讨论】:

  • 您可以尝试此SO thread 中的其他建议以下载 JSON 文件。将 JSON 文件名重命名为 client_secrets.json,然后保存到 bin/Debug ot bin/Release 与目录相同的 *.exe 中。此外,您的网络速度可能很慢,如 related issue 中所述。
  • 我尝试重命名这些文件并将其放置在 bin/Release 中,但没有成功。

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


【解决方案1】:

十五分钟对于超时值来说太长了。我用一分钟。

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

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

通过更短的超时时间和恢复上传的代码,您的上传在上传服务器繁忙时会更快。

【讨论】:

  • 是的,你是对的。但我想同时运行多个线程进行上传。你能告诉我 IUploadSessionData 中uploaduri 的哪些属性我需要保存在数据库中以供多个进程使用。
  • 我不想将 Upload Uri 的所有属性保存在数据库中以供简历
  • 您应该查看 GitHub 中的示例程序。我保存 .UploadUri.AbsoluteUri 和正在上传的文件的完整路径名。
【解决方案2】:

终于成功了

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

我以错误的方式提供 TimeSpan。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-07
    • 2014-10-13
    • 2014-11-03
    • 2014-12-30
    • 1970-01-01
    • 1970-01-01
    • 2019-06-17
    • 2016-05-07
    相关资源
    最近更新 更多