首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >YouTube-API上传文件,没有任何错误(C#)

YouTube-API上传文件,没有任何错误(C#)
EN

Stack Overflow用户
提问于 2014-02-04 17:40:26
回答 1查看 2.2K关注 0票数 5

我正在使用谷歌的.net nuget (Google.Apis.YouTube.v3)进行API调用。到目前为止,一切都很正常(获取播放列表、视频信息等等)。但是当我试图上传一段视频时,它在几秒钟内就完成了,什么也没有发生。upload_ResponseReceived从不被调用,upload_ProgressChanged只被调用两次,输出如下:( 1)在Bytes sent: 0, Status: Failed, Exception: System.Threading.Tasks.TaskCanceledException: A task was canceled.之后的Bytes sent: 0, Status: Starting, Exception:

样本代码:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Upload;
using Google.Apis.Util.Store;
using Google.Apis.YouTube.v3;
using Google.Apis.YouTube.v3.Data;

namespace UploadTest
{
    class Program
    {
        static void Main(string[] args)
        {
            UserCredential credential;
            using (FileStream stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
            {
                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    new[] { YouTubeService.Scope.Youtube, YouTubeService.Scope.YoutubeUpload },
                    "user",
                    CancellationToken.None,
                    new FileDataStore("YouTube.Auth.Store")).Result;
            }
            var youtubeService = new YouTubeService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
            });
            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 = @"E:\video.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;
                videosInsertRequest.Upload();
            }
            Console.ReadLine();
        }

        private static void videosInsertRequest_ResponseReceived(Video obj)
        {
            Debug.WriteLine("Video has been uploaded! ID: {0}",obj.Id);
        }

        private static void videosInsertRequest_ProgressChanged(IUploadProgress obj)
        {
            Debug.WriteLine("Bytes sent: {0}, Status: {1}, Exception: {2}", obj.BytesSent, obj.Status, obj.Exception);
        }
    }
}

堆栈跟踪:

代码语言:javascript
复制
System.AggregateException was unhandled.
  HResult=-2146233088
  Message=One or more errors occurred.
  Source=mscorlib
  StackTrace:
       at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
       at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
       at System.Threading.Tasks.Task`1.get_Result()
       at Google.Apis.Upload.ResumableUpload`1.Upload() in c:\code\google.com\google-api-dotnet-client\default\Tools\Google.Apis.Release\bin\Debug\output\default\Src\GoogleApis\Apis\[Media]\Upload\ResumableUpload.cs:line 351
       at UploadTest.Program.<>c__DisplayClass2.<Main>b__1() in e:\Development\UploadTest\Program.cs:line 52
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.Threading.Tasks.TaskCanceledException
       HResult=-2146233029
       Message=A task was canceled.
       InnerException: 

知道我可能做错什么了吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-06 18:01:39

好的,我认为您有连接问题,您所要做的就是将块大小更改为更小的大小,所以您的代码应该如下所示:

const = 0x400;var minimumChunkSize = 256 * KB;var videosInsertRequest =youtubeService.Videos.Insert(视频、“代码段、状态”、fileStream、“视频/*”);videosInsertRequest.ProgressChanged += videosInsertRequest_ProgressChanged;videosInsertRequest.ResponseReceived += videosInsertRequest_ResponseReceived;//默认块大小为10 1MB,这里将使用1MB。videosInsertRequest.ChunkSize = minimumChunkSize * 4;videosInsertRequest.Upload();

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21559550

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档