c# 使用 Youtube data api v3 获取 youtube 播放列表

标签 c# youtube youtube-data-api

我目前正在为我的学校开发一个软件。该软件必须能够发布视频,但它还必须能够将这些视频添加到播放列表中。
问题:如何列出帐户的所有现有播放列表(甚至是空的或私有(private)的)?我正在使用 YouTube 数据 API v3,但无法获取空的。
这是代码:

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 read-only access to the authenticated 
                // user's account, but not other types of account access.
                new[] { YouTubeService.Scope.YoutubeReadonly },
                "user",
                CancellationToken.None,
                new FileDataStore(this.GetType().ToString())
            );
        }

        var youtubeService = new YouTubeService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = this.GetType().ToString()
        });

        var channelsListRequest = youtubeService.Channels.List("contentDetails");
        channelsListRequest.Mine = true;

        // Retrieve the contentDetails part of the channel resource for the authenticated user's channel.
        var channelsListResponse = await channelsListRequest.ExecuteAsync();

        foreach (var channel in channelsListResponse.Items)
        {
            // From the API response, extract the playlist ID that identifies the list
            // of videos uploaded to the authenticated user's channel.
            var uploadsListId = channel.ContentDetails.RelatedPlaylists.Uploads;

            Console.WriteLine("Videos in list {0}", uploadsListId);

            var nextPageToken = "";
            while (nextPageToken != null)
            {
                var playlistItemsListRequest = youtubeService.PlaylistItems.List("snippet");
                playlistItemsListRequest.PlaylistId = uploadsListId;
                playlistItemsListRequest.MaxResults = 50;
                playlistItemsListRequest.PageToken = nextPageToken;

                // Retrieve the list of videos uploaded to the authenticated user's channel.
                var playlistItemsListResponse = await playlistItemsListRequest.ExecuteAsync();

                foreach (var playlistItem in playlistItemsListResponse.Items)
                {
                    // Print information about each video.
                    Console.WriteLine("{0} ({1})", playlistItem.Snippet.Title, playlistItem.Snippet.ResourceId.VideoId);
                }

                nextPageToken = playlistItemsListResponse.NextPageToken;
            }
        }
    }

先感谢您。

最佳答案

通过这样做,我得到了这段代码:

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 read-only access to the authenticated 
                // user's account, but not other types of account access.
                new[] { YouTubeService.Scope.YoutubeReadonly },
                "user",
                CancellationToken.None,
                new FileDataStore(this.GetType().ToString())
            );
        }

        var youtubeService = new YouTubeService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = this.GetType().ToString()
        });

        // var channelsListRequest = youtubeService.Channels.List("contentDetails");
        var playlistListRequest = youtubeService.Playlists.List("snippet");
        playlistListRequest.Mine = true;

        // Retrieve the contentDetails part of the channel resource for the authenticated user's channel.
        var playlistListResponse = await playlistListRequest.ExecuteAsync();

        foreach (var playlist in playlistListResponse.Items)
        {
            // From the API response, extract the playlist ID that identifies the list
            // of videos uploaded to the authenticated user's channel.
            var playlistListId = playlist.Id;

            Console.WriteLine("Videos in list {0}", playlistListId);
        }
    }

感谢“not_a_bot”帮助我。

关于c# 使用 Youtube data api v3 获取 youtube 播放列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44295720/

相关文章:

每 10 分钟更新一次 YouTube 缩略图的 Python 代码在大约一个小时后失败——我需要使用刷新 token 吗?

c# - 当我们想要历史时,阅读事件溯源是否正确?

c# - 从命令行运行时 mstest v2 忽略 ExpectedException

c# - sql server 2008获取表主键的方法

facebook - 如何在youtube等Facebook上分享我的网站中的视频

youtube-data-api - youtube api C# 视频完整元数据

c# - 更新 Active Directory 用户登录名域

javascript - 使用 onclick 事件暂停/停止嵌入的 YouTube 视频?

youtube - 使用Youtube API代替Youtube抓取工具

jquery - 网站中的 Youtube 设备支持消息