使用YouTube Data API v3上传的视频未在YouTube网站上显示视频文件名

时间:2014-06-18 16:00:03

标签: c# youtube-api filenames

我们遇到了与上传的视频文件名(原始文件)相关的问题,该问题在YouTube网站上显示“未知”,而不是上传的文件名。

我们正在使用YouTube Data API v3与来自ASP.Net C#网络应用程序的YouTube进行交互,该应用程序使用适用于.NET的Google API客户端库。

在测试过程中,我们发现使用YouTube Data API v3上传的所有视频都没有在YouTube网站的“视频信息”部分下显示视频文件名(原始文件)。我们在之前的YouTube Data API v2中没有看到此问题。

仅供参考我们已按照YouTube .Net示例代码上传视频https://developers.google.com/youtube/v3/docs/videos/insert

我们未找到有关YouTube Data API v3参考指南的任何信息,这些信息可帮助我们了解如何在上传时设置视频文件名。

有人可以就上述问题提出建议吗?

示例代码

using System;
using System.IO;
using System.Reflection;
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 Google.Apis.YouTube.Samples
{
    /// <summary>
    /// YouTube Data API v3 sample: upload a video.
   /// Relies on the Google APIs Client Library for .NET, v1.7.0 or higher.
   /// See https://code.google.com/p/google-api-dotnet-client/wiki/GettingStarted
  /// </summary>
  internal class UploadVideo
  {
  [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 = @"REPLACE_ME.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);
}

} }

0 个答案:

没有答案