asp.net-mvc - 使用Google.Apis.YouTube.v3在YouTube channel 上载视频而无最终用户登录提示

标签 asp.net-mvc youtube youtube-data-api google-api-dotnet-client google-data-api

我想使用Google.Apis.YouTube.v3在YouTube上上传视频。
我的要求是在YouTube channel 上上传视频而没有最终用户登录提示。
因此,请告诉我有什么方法可以验证服务器端的用户并将视频直接上传到我在YouTube中的 channel 。
我想在MVC C#应用程序中实现

最佳答案

[HttpPost]
public async Task<ActionResult> UploadVideo(HttpPostedFileBase video, UploadVideoInfo info)
  {
    try
    {
      var httpPostedFile = Request.Files[0];
      if (httpPostedFile != null)
      {
        UserCredential credential;
        using (var stream = new FileStream("client_secret.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 = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name
      });

      var videos = new Video();
      videos.Snippet = new VideoSnippet();
      videos.Snippet.Title = info.VideoTitle;
      videos.Snippet.Description = info.VideoDescription;
      videos.Snippet.Tags = new string[] { "tag1", "tag2" };
      videos.Snippet.CategoryId = "22";
      videos.Status = new VideoStatus();
      videos.Status.PrivacyStatus = "public"; // or "public" or "unlisted"
      var videosInsertRequest = youtubeService.Videos.Insert(videos, "snippet,status", video.InputStream, "video/*");
      videosInsertRequest.ProgressChanged += videosInsertRequest_ProgressChanged;
      videosInsertRequest.ResponseReceived += videosInsertRequest_ResponseReceived;

      await videosInsertRequest.UploadAsync();

    }
  }
  catch (Exception ex)
  {

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

关于asp.net-mvc - 使用Google.Apis.YouTube.v3在YouTube channel 上载视频而无最终用户登录提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33451618/

相关文章:

.net - 在 asp.net MVC 中发布后维护 URL

jquery - 与YouTube一起嵌入的媒体无法正常工作-错误:不支持 “type”的指定 “video/youtube”属性

youtube - 我们如何使用 youtube/v3/videos?id=<> 调用确定 YouTube 视频已获利?

api - 如何从 channel 中获取前10个视频,并在响应中包含观看次数

android - Youtube 如何在按下主页按钮后创建小窗口播放器?

javascript - 如何通过 youtube-api 获取包含给定视频 ID 的播放列表 ID

asp.net-mvc - @helper 和 Url.Action

c# - Foreach 找不到嵌套类

asp.net-mvc - 将文件 obj\Debug\build.force 复制到 obj\Release\Package\PackageTmp\obj\Debug\build.force 失败。找不到文件 'obj\Debug\build.force'

javascript - YouTube API json 网址