上传字幕使用YouTube API v3 (dotnet, c#, Null错误,具有挑战性)

本文关键字:Null 错误 挑战性 dotnet 字幕 YouTube v3 API | 更新日期: 2023-09-27 18:06:28

我正在尝试使用oauth2与youtube dotnet库上传封闭字幕。这些示例不包括。net,我知道这是新的(即API中很少使用的功能)。我在这个问题上很纠结,不知道我是否遗漏了什么,或者API中是否有bug。请帮助!

得到的错误是: BytesSent: 0 Exception: {"Value cannot be null.'r'nParameter name: baseUri"} Status: Failed

我的代码可以上传视频,但在上传标题时给出上述错误。

这里是代码,其中包括两个工作视频上传和破碎的标题上传。

// THIS ONE WORKS !!!!
        private void cmdUploadVideo_Click(object sender, EventArgs e)
    {
        YouTubeService youtubeService = getYouTubeService();
        Video newVideo = new Video();
        newVideo.Snippet = new VideoSnippet();
        newVideo.Snippet.Title = "Learning YouTube API 2";
        newVideo.Snippet.Description = "Description of video";
        newVideo.Snippet.Tags = new string[] { "tag1", "tag2" };
        newVideo.Snippet.CategoryId = "22"; // See https://developers.google.com/youtube/v3/docs/videoCategories/list
        newVideo.Status = new VideoStatus();
        newVideo.Status.PrivacyStatus = "unlisted"; // or "private" or "public"
        var filePath = @"c:''_IM''TestVideo.mp4"; 
        using (var fileStream = new FileStream(filePath, FileMode.Open))
        {
            VideosResource.InsertMediaUpload videosInsertRequest = youtubeService.Videos.Insert(newVideo, "snippet,status", fileStream, "video/*");
            IUploadProgress results = videosInsertRequest.Upload();
        }
    }
// HELP PLEASE!!! VERY SIMILAR STRUCTURE BUT GETS ERROR
    private void cmdUploadVideoCaption_Click(object sender, EventArgs e)
    {
        YouTubeService youtubeService = getYouTubeService();
        Caption newCaption = new Caption();
        newCaption.Snippet = new CaptionSnippet();
        newCaption.Snippet.Name = "test";
        newCaption.Snippet.Language = "en";
        newCaption.Snippet.VideoId = txtVideoID.Text;
        var filePath = @"c:''_IM''captions.txt"; // Replace with path to actual movie file.
        using (var fileStream = new FileStream(filePath, FileMode.Open))
        {
            CaptionsResource.InsertMediaUpload captionInsertRequest = youtubeService.Captions.Insert(newCaption, "snippet,status", fileStream, "*/*");
            captionInsertRequest.Sync = true;
            IUploadProgress result = captionInsertRequest.Upload();
// HELP WITH LINE ABOVE ... THAT IS WHERE THE ERROR OCCURS
        }
    }
// THIS PART WORKS !!!! IT IS USED BY BOTH, OAUTH2
    private YouTubeService getYouTubeService()
    {
        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, YouTubeService.Scope.YoutubeForceSsl },
                "user",
                CancellationToken.None,
                new FileDataStore("YouTube.Auth.Store")).Result;
        }
        YouTubeService youtubeService = new YouTubeService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
        });
        return youtubeService;
    }
下面是上面代码的错误输出:
?result
{Google.Apis.Upload.ResumableUpload<Google.Apis.YouTube.v3.Data.Caption>.ResumableUploadProgress}
    [Google.Apis.Upload.ResumableUpload<Google.Apis.YouTube.v3.Data.Caption>.ResumableUploadProgress]: {Google.Apis.Upload.ResumableUpload<Google.Apis.YouTube.v3.Data.Caption>.ResumableUploadProgress}
    BytesSent: 0
    Exception: {"Value cannot be null.'r'nParameter name: baseUri"}
    Status: Failed
?result.Exception
{"Value cannot be null.'r'nParameter name: baseUri"}
    [System.ArgumentNullException]: {"Value cannot be null.'r'nParameter name: baseUri"}
    _className: null
    _data: null
    _dynamicMethods: null
    _exceptionMethod: null
    _exceptionMethodString: null
    _helpURL: null
    _HResult: -2147467261
    _innerException: null
    _ipForWatsonBuckets: 1903238454
    _message: "Value cannot be null."
    _remoteStackIndex: 0
    _remoteStackTraceString: null
    _safeSerializationManager: {System.Runtime.Serialization.SafeSerializationManager}
    _source: null
    _stackTrace: {sbyte[96]}
    _stackTraceString: null
    _watsonBuckets: null
    _xcode: -532462766
    _xptrs: 0
    Data: {System.Collections.ListDictionaryInternal}
    HelpLink: null
    HResult: -2147467261
    InnerException: null
    IPForWatsonBuckets: 1903238454
    IsTransient: false
    Message: "Value cannot be null.'r'nParameter name: baseUri"
    RemoteStackTrace: null
    Source: "Microsoft.Threading.Tasks"
    StackTrace: "   at Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)'r'n   at Microsoft.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(Task task)'r'n   at Microsoft.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)'r'n   at Microsoft.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()'r'n   at Google.Apis.Upload.ResumableUpload`1.<UploadCoreAsync>d__e.MoveNext() in c:''code''github''google-api-dotnet-client''Tools''Google.Apis.Release''bin''Release''1.9.2''default''Src''GoogleApis''Apis''[Media]''Upload''ResumableUpload.cs:line 460"
    TargetSite: {Void ThrowForNonSuccess(System.Threading.Tasks.Task)}
    WatsonBuckets: null

注意,我尝试这样做作为一个异步任务,并得到同样的错误,但也许输出将更有信息调试,因为它包括在ResumableUpload行号。以下是进行异步调用时的错误输出(代码未显示):

A first chance exception of type 'System.Net.Http.HttpRequestException' occurred in System.Net.Http.dll
A first chance exception of type 'System.Net.Http.HttpRequestException' occurred in Microsoft.Threading.Tasks.dll
An error prevented the upload from completing.
System.Net.Http.HttpRequestException: Response status code does not indicate success: 403 (Forbidden).
   at Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at Microsoft.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(Task task)
   at Microsoft.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at Microsoft.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Google.Apis.Upload.ResumableUpload`1.<UploadAsync>d__0.MoveNext() in c:'code'github'google-api-dotnet-client'Tools'Google.Apis.Release'bin'Release'1.9.2'default'Src'GoogleApis'Apis'[Media]'Upload'ResumableUpload.cs:line 378
A first chance exception of type 'System.ArgumentNullException' occurred in System.dll
A first chance exception of type 'System.ArgumentNullException' occurred in Microsoft.Threading.Tasks.dll
An error prevented the upload from completing.
System.ArgumentNullException: Value cannot be null.
Parameter name: baseUri
   at Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at Microsoft.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(Task task)
   at Microsoft.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at Microsoft.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Google.Apis.Upload.ResumableUpload`1.<UploadCoreAsync>d__e.MoveNext() in c:'code'github'google-api-dotnet-client'Tools'Google.Apis.Release'bin'Release'1.9.2'default'Src'GoogleApis'Apis'[Media]'Upload'ResumableUpload.cs:line 460

顺便说一句,我在2015年8月3日晚上更新了我的nuget youtube库,所以我使用的是我可用的最新代码。

感谢任何可以帮助!!!!的人

上传字幕使用YouTube API v3 (dotnet, c#, Null错误,具有挑战性)

您的代码帮助我解决了我在执行相同任务时遇到的单独问题。至于你的错误,我认为是由于

"snippet/status"

参数在Insert()方法中。似乎API期望代码片段和状态对象(您正在使用cmdUploadVideo_Click()传递)方法。下面的代码为我工作…

    YouTubeService youtubeService = GetYouTubeService();
    Caption caption = new Caption();
    caption.Snippet = new CaptionSnippet();
    caption.Snippet.Name = "Test Caption";
    caption.Snippet.Language = "en";
    caption.Snippet.VideoId = "videoId";
    caption.Snippet.IsDraft = false;
    var filePath = "{filepath}";
    using (var fileStream = new FileStream(filePath, FileMode.Open))
    {
        CaptionsResource.InsertMediaUpload captionInsertRequest = youtubeService.Captions.Insert(caption, "snippet", fileStream, "*/*");
        captionInsertRequest.Sync = true;
        captionInsertRequest.ProgressChanged += captionInsertRequest_ProgressChanged;
        captionInsertRequest.ResponseReceived += captionInsertRequest_ResponseReceived;
        IUploadProgress result = captionInsertRequest.Upload();
    }

希望这对你有帮助。谢谢。

除了@mpwhitt提到的。请确保您的标题文件(@"c:'_IM'captions.txt")是正确的。为了确保您是使用示例标题文件还是使用创建标题文件的代码:

File.WriteAllText( "example.srt",
@"1
00:00:00.00 --> 00:00:03.00
This is just an example
2
00:00:03.00 --> 00:00:07.00
and we'll see if it works
" );

我看到你设置你需要的范围(YouTubeService.Scope.YoutubeForceSsl)根据文档(https://developers.google.com/youtube/v3/docs/captions/insert)在我的使用中,我使用了另一个(YouTubePartner);但两者都应该根据文档工作。

设置了captionInsertRequest。同步到false。我想当它设为true时,它需要处理。尽管如此,当它被设为true时,它不应该出错。但如果将其设为false,它就会成功并立即显示在视频中。