如何使用youtube api在c#dotnet应用程序中创建youtube播放列表?

如何使用youtube api在c#dotnet应用程序中创建youtube播放列表?,c#,youtube-api,C#,Youtube Api,我正在使用ASP.NET MVC应用程序,我需要通过Youtube api V3添加Youtube播放列表 我想通过OAuth2.0在我的youtube帐户中添加一个播放列表 请参阅下面我的示例 using System; using System.IO; using System.Reflection; using System.Threading; using System.Threading.Tasks; using Google.Apis.Auth.OAuth2; using Goog

我正在使用ASP.NET MVC应用程序,我需要通过Youtube api V3添加Youtube播放列表

我想通过OAuth2.0在我的youtube帐户中添加一个播放列表

请参阅下面我的示例

using System;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;

using Google.Apis.Auth.OAuth2;
using Google.Apis.Auth.OAuth2.Flows;
using Google.Apis.Auth.OAuth2.Responses;
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>
  /// </summary>
  internal class PlaylistUpdates
  {
    [STAThread]
    static void Main(string[] args)
    {
      Console.WriteLine("YouTube Data API: Playlist Updates");
      Console.WriteLine("==================================");

      try
      {
        new PlaylistUpdates().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 for full read/write access to the
                  // authenticated user's account.
                  new[] { YouTubeService.Scope.Youtube },
                  "user",
                  CancellationToken.None,
                  new FileDataStore(this.GetType().ToString())
              );
            }      
            var youtubeService = new YouTubeService(new BaseClientService.Initializer()
            {
              HttpClientInitializer = credential,
              ApplicationName = this.GetType().ToString()
            });            
            // Create a new, private playlist in the authorized user's channel.
            var newPlaylist = new Playlist();
      newPlaylist.Snippet = new PlaylistSnippet();
      newPlaylist.Snippet.Title = "Test Playlist";
      newPlaylist.Snippet.Description = "A playlist created with the YouTube API v3";
      newPlaylist.Status = new PlaylistStatus();
      newPlaylist.Status.PrivacyStatus = "public";
      newPlaylist = await youtubeService.Playlists.Insert(newPlaylist, "snippet,status").ExecuteAsync();

      // Add a video to the newly created playlist.
      var newPlaylistItem = new PlaylistItem();
      newPlaylistItem.Snippet = new PlaylistItemSnippet();
      newPlaylistItem.Snippet.PlaylistId = newPlaylist.Id;
      newPlaylistItem.Snippet.ResourceId = new ResourceId();
      newPlaylistItem.Snippet.ResourceId.Kind = "youtube#video";
      newPlaylistItem.Snippet.ResourceId.VideoId = "GNRMeaz6QRI";
      newPlaylistItem = await youtubeService.PlaylistItems.Insert(newPlaylistItem, "snippet").ExecuteAsync();

      Console.WriteLine("Playlist item id {0} was added to playlist id {1}.", newPlaylistItem.Id, newPlaylist.Id);
    }
  }
}
我是youtube api的新手。我应该在代码中做什么

我从这里抓取了菜单和卡片的示例代码:

使用

{
  "application type": "console application", 
  ".Net framework": "4.5"  ,
  "language" : C#
}
我面临的问题是以下函数中出现错误:

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 for full read/write access to the
              // authenticated user's account.
              new[] { YouTubeService.Scope.Youtube },
              "user",
              CancellationToken.None,
              new FileDataStore(this.GetType().ToString())
          );
        }   
上面显示了以下错误:

    400. That’s an error.

    Error: redirect_uri_mismatch
    The redirect URI in the request, http://127.0.0.1:1492/authorize/, does not match the ones authorized for the OAuth client. To update the authorized redirect URIs, visit: https://console.developers.google.com/apis/credentials/oauthclient/yourclientid.apps.googleusercontent.com?project=96215660321
    Learn more
    Request Details
    access_type=offline
response_type=code
client_id=96215660321-yourclientid.apps.googleusercontent.com
redirect_uri=http://127.0.0.1:1492/authorize/
scope=https://www.googleapis.com/auth/youtube
我试图阅读谷歌的文章并查找示例实现,但无法很好地解决/解释这个问题

此外,我还通过创建Credentails添加了OAuth2.0客户端ID,然后选择应用程序类型作为Web应用程序


然后我在授权重定向uri下添加,并按下保存按钮,它重定向到重定向_uri=not(注意端口)。所以这是一个错误,可能是在你的授权下,schgab所说的。还记得GoogleWebAuthorizationBroker.AuthorizationAsync用于安装的应用程序,它不会在服务器上运行。我在visual studio和google OAuth credential中已将端口固定为44373,但google每次都会添加一个新端口。我不知道生成端口的模式。我需要上传、阅读、搜索和删除youtube视频。你们有什么建议吗?我应该使用api键来执行这些任务吗?它重定向到redirect_uri=not(注意端口)。所以这是一个错误,可能是在你的授权下,schgab所说的。还记得GoogleWebAuthorizationBroker.AuthorizationAsync用于安装的应用程序,它不会在服务器上运行。我在visual studio和google OAuth credential中已将端口固定为44373,但google每次都会添加一个新端口。我不知道生成端口的模式。我需要上传、阅读、搜索和删除youtube视频。你们有什么建议吗?我应该使用api密钥来完成这些任务吗?
    400. That’s an error.

    Error: redirect_uri_mismatch
    The redirect URI in the request, http://127.0.0.1:1492/authorize/, does not match the ones authorized for the OAuth client. To update the authorized redirect URIs, visit: https://console.developers.google.com/apis/credentials/oauthclient/yourclientid.apps.googleusercontent.com?project=96215660321
    Learn more
    Request Details
    access_type=offline
response_type=code
client_id=96215660321-yourclientid.apps.googleusercontent.com
redirect_uri=http://127.0.0.1:1492/authorize/
scope=https://www.googleapis.com/auth/youtube