【问题标题】:YouTube API v3, C# MVC - Upload video to MY channel with OAuthYouTube API v3、C# MVC - 使用 OAuth 将视频上传到我的频道
【发布时间】:2018-02-06 05:09:15
【问题描述】:

我在 MVC 网络应用程序中使用 YouTube API V3。目的是让我的网络用户使用我的 OAuth 凭据将视频上传到我的 YouTube 频道。用户将他们的视频上传到我的网络服务器后,我需要在我的网络服务器和 YouTube 之间自动上传,无需用户干预。

我的(初稿)代码如下。我的问题是:

  1. 更新 - 已解决。 每当我尝试删除视频时,都会收到“未经授权”错误。当我检查令牌的范围时,它只有“YoutubeUpload”而不是“Youtube”。有没有办法解决这个问题?

  2. 我创建了一个“Other”类型的 OAuth 客户端凭据并导出了 JSON(我指定了“Other”而不是“Web 应用程序” - 这是正确的吗?):https://console.developers.google.com/apis/credentials

当我尝试上传视频时,我被重定向到“授权应用程序”,并显示“我的公司想要访问您的 YouTube 频道”。问题是,我不想访问任何人的频道。我只是希望他们能够上传到我的频道。这可能吗?如果可以,怎么办?

    public async Task<string> UploadVideoToYouTube(Stream filestream)
    {
        UserCredential credential;

        string cs = HttpContext.Current.Server.MapPath("~/client_secrets.json");

        using (var stream = new FileStream(cs, 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.Youtube,
                    YouTubeService.Scope.YoutubeUpload },
                "user",
                CancellationToken.None
            );
        }

       //check scope with this URL
        //https://www.googleapis.com/oauth2/v1/tokeninfo?access_token={ACCESS-TOKEN}
        // This bit checks if the token is out of date, 
        // and refreshes the access token using the refresh token.
        if (credential.Token.IsExpired(SystemClock.Default))
        {
            if (!await credential.RefreshTokenAsync(CancellationToken.None))
            {
                Console.WriteLine("No valid refresh token.");
            }
        }

        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" };
        //https://gist.github.com/dgp/1b24bf2961521bd75d6c
        //26 - How-to & Style
        video.Snippet.CategoryId = "26"; // See https://developers.google.com/youtube/v3/docs/videoCategories/list
        video.Status = new VideoStatus();
        video.Status.PrivacyStatus = "private"; // "unlisted" or "private" or "public"

        var videosInsertRequest = youtubeService.Videos.Insert(video, "snippet,status", filestream, "video/*");
        videosInsertRequest.ProgressChanged += videosInsertRequest_ProgressChanged;
        videosInsertRequest.ResponseReceived += videosInsertRequest_ResponseReceived;

        await videosInsertRequest.UploadAsync();

        return videosInsertRequest.ResponseBody.Id;
    }

    void videosInsertRequest_ResponseReceived(Video video)
    {
        //http://www.youtube.com/watch?v={VIDEO-ID}
        Console.WriteLine("Video id '{0}' was successfully uploaded.", video.Id);

    }

    public async Task DeleteVideoFromYouTube(string id)
    {
        UserCredential credential;

        string cs = HttpContext.Current.Server.MapPath("~/client_secrets.json");

        using (var stream = new FileStream(cs, 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.Youtube,                    
                YouTubeService.Scope.YoutubeUpload  },
                "user",
                CancellationToken.None
            );
        }

        // This bit checks if the token is out of date, 
        // and refreshes the access token using the refresh token.
        if (credential.Token.IsExpired(SystemClock.Default))
        {
            if (!await credential.RefreshTokenAsync(CancellationToken.None))
            {
                Console.WriteLine("No valid refresh token.");
            }
        }

        var youtubeService = new YouTubeService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
        });

        var videosDeleteRequest = youtubeService.Videos.Delete(id);
        await videosDeleteRequest.ExecuteAsync();


    }

    void videosInsertRequest_ProgressChanged(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;
        }
    }

更新第一个问题的解决方案是转到此处:https://myaccount.google.com/permissions 并删除授权的 YouTube 应用程序。它必须在之前没有 YouTubeService.Scope.Youtube 范围的情况下获得授权。

但是,当我删除帐户并再次运行我的 Web 应用程序时,它提示我再次使用 OAuth 进行身份验证。我不希望它这样做。我希望它自动以我身份进行身份验证(对于所有用户)。这是我的第二个没有答案的问题....

【问题讨论】:

  • 你得到答案了吗?
  • 如何上传到我的 Youtube 频道?

标签: asp.net-mvc asp.net-mvc-5 youtube-data-api


【解决方案1】:

关键在这部分代码:

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

您必须指定正确的用户名,而不仅仅是“用户”

【讨论】:

  • 可能是 OP 故意混淆了他们的用户名。
【解决方案2】:

这是在 YouTube 上上传视频的示例代码

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://developers.google.com/api-client-library/dotnet/get_started
  /// </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);
    }
  }
}

【讨论】:

  • 感谢您的回复......但我不完全确定这如何帮助解决我的问题......
猜你喜欢
  • 2019-02-28
  • 2014-11-22
  • 2018-02-02
  • 2015-06-27
  • 2015-08-31
  • 2012-12-23
  • 1970-01-01
  • 2013-10-27
  • 2013-02-03
相关资源
最近更新 更多