尝试使用youtube API v3为c#上传视频时获取400错误请求

时间:2015-06-15 14:07:58

标签: c# youtube-api

使用以下代码:

    public async Task<YouTubeService> GetYouTubeService(string userEmail)
    {
        UserCredential credential;
        using (var stream = new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
        {
            GoogleWebAuthorizationBroker.Folder = "Tasks.Auth.Store";
            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets,
                new[]
                {
                    YouTubeService.Scope.Youtube,
                    YouTubeService.Scope.Youtubepartner,
                    YouTubeService.Scope.YoutubeUpload,
                    YouTubeService.Scope.YoutubepartnerChannelAudit,
                    YouTubeService.Scope.YoutubeReadonly
                },
                userEmail,
                CancellationToken.None,
                new FileDataStore("YouTube.Auth.Store")).Result;
        }

        var youtubeService = new YouTubeService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = this.GetType().ToString()
        });

        return youtubeService;
    }

            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";
            video.Status = new VideoStatus();
            video.Status.PrivacyStatus = "public";
            var filePath = @"c:\temp\nouvelair.mp4";

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


    public 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;
            case UploadStatus.Completed:
                Console.WriteLine("An error prevented the upload from completing.\n{0}", progress.Exception);
                break;
        }
    }

在函数videosInsertRequest_ProgressChanged中,首先我得到视频的状态,然后在我获得失败状态后立即出现以下异常:

响应状态代码不表示成功:400(错误请求)。

1 个答案:

答案 0 :(得分:1)

您正尝试在不存在的部分中设置值;你有,作为你的部分论点:

snippet.status

但是你需要用逗号分隔部分,而不是点:

var videosInsertRequest = youtubeService.Videos.Insert(video, "snippet,status", fileStream, "video/*");