【问题标题】:Upload a video do not work with the YouTube API v3上传视频不适用于 YouTube API v3
【发布时间】:2014-10-13 23:47:35
【问题描述】:
    [STAThread]
    static void Main(string[] args)
    {
        Console.WriteLine("YouTube Data API: Upload Video");
        Console.WriteLine("==============================");

        try
        {
            new UploadVideo().Run().Wait();
        }
        catch (AggregateException ex)
        {
            foreach (var e in ex.InnerExceptions)
            {
                Console.WriteLine("Error: " + e.Message);
            }
        }

        Console.WriteLine("Press any key to continue...");
        Console.ReadKey();
    }

    private async Task Run()
    {
        UserCredential credential;
        using (var stream = new FileStream("client_secrets.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 },
                "user",
                CancellationToken.None
            );
        }

        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 = "video2.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();
        }
    }

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

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

但我正在取得进展。状态每次都失败。我无法将视频上传到 youtube。我的视频文件大小为 2MB。我在 https://console.developers.google.com 中为 web 应用程序创建客户端 ID 和密钥。谁能帮我解决这个问题。

提前致谢

【问题讨论】:

    标签: c# .net


    【解决方案1】:

    您已为 Web 应用程序创建了客户端 ID 和密钥。 必须创建安装密钥。并下载 JSON 文件。 将 JSON 文件名重命名为“client_secrets.json”。 并保存到 bin/Debug ot bin/Release 与 *.exe 目录相同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-30
      • 2012-10-07
      • 2012-12-23
      • 1970-01-01
      • 2012-10-27
      • 2017-06-14
      • 2014-11-03
      • 1970-01-01
      相关资源
      最近更新 更多